Skip to content

Commit 85b1535

Browse files
authored
[20250203] BOJ / 골드2 / 캔디캔디 / 권혁준
1 parent 24545b0 commit 85b1535

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
import java.math.BigInteger;
6+
7+
class Main {
8+
9+
// IO field
10+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
static StringTokenizer st;
13+
14+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
15+
static int nextInt() {return Integer.parseInt(st.nextToken());}
16+
static long nextLong() {return Long.parseLong(st.nextToken());}
17+
static void bwEnd() throws Exception {bw.flush();bw.close();}
18+
19+
// Additional field
20+
21+
22+
public static void main(String[] args) throws Exception {
23+
24+
nextLine();
25+
long M = nextInt();
26+
int N = nextInt();
27+
Integer[] A = new Integer[N];
28+
for(int i=0;i<N;i++) A[i] = Integer.parseInt(br.readLine());
29+
Arrays.sort(A, Comparator.reverseOrder());
30+
31+
for(int i=1;i<N;i++) {
32+
long cnt = i, diff = A[i-1] - A[i];
33+
34+
if(M >= cnt * diff) {
35+
M -= cnt*diff;
36+
}
37+
else {
38+
for(int j=0;j<M%cnt;j++) A[j] = A[i-1] - (int)(M/cnt + 1);
39+
for(int j=(int)(M%cnt);j<i;j++) A[j] = A[i-1] - (int)(M/cnt);
40+
M = 0;
41+
break;
42+
}
43+
}
44+
45+
if(M != 0) {
46+
for(int j=0;j<M%N;j++) A[j] = A[N-1] - (int)(M/N + 1);
47+
for(int j=(int)(M%N);j<N;j++) A[j] = A[N-1] - (int)(M/N);
48+
}
49+
50+
BigInteger ans = new BigInteger("0");
51+
for(int i=0;i<N;i++) {
52+
BigInteger temp = new BigInteger(A[i].toString());
53+
ans = ans.add(temp.multiply(temp));
54+
}
55+
bw.write(ans.mod(new BigInteger("18446744073709551616"))+" ");
56+
57+
bwEnd();
58+
}
59+
60+
}
61+
62+
```

0 commit comments

Comments
 (0)