Skip to content

Commit 6a3b637

Browse files
authored
[20250327] BOJ / P2 / King's Roads / 권혁준
1 parent f24ecee commit 6a3b637

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Node{
7+
int s, e;
8+
long cost;
9+
Node(int s, int e, long cost){
10+
this.s = s;
11+
this.e = e;
12+
this.cost = cost;
13+
}
14+
}
15+
16+
class Main {
17+
18+
// IO field
19+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
21+
static StringTokenizer st = new StringTokenizer("");
22+
23+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
24+
static String nextToken() throws Exception {
25+
while(!st.hasMoreTokens()) nextLine();
26+
return st.nextToken();
27+
}
28+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
29+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
30+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
31+
static void bwEnd() throws Exception {bw.flush();bw.close();}
32+
33+
// Additional field
34+
35+
static int N;
36+
static long M;
37+
static long[] A;
38+
39+
static int[] r;
40+
41+
public static void main(String[] args) throws Exception {
42+
43+
ready();
44+
solve();
45+
46+
bwEnd();
47+
48+
}
49+
50+
static void ready() throws Exception{
51+
52+
N = nextInt();
53+
M = nextInt();
54+
A = new long[N];
55+
r = new int[N];
56+
for(int i=0;i<N;i++) {
57+
A[i] = nextLong();
58+
r[i] = i;
59+
}
60+
61+
}
62+
63+
static void solve() throws Exception{
64+
65+
if(N == 1) {
66+
bw.write("0\n");
67+
return;
68+
}
69+
70+
Arrays.sort(A);
71+
int s = 0, e = N-1, cnt = 0;
72+
long ans = 0;
73+
while(cnt<N-1 && A[s]+A[e]<M) {
74+
ans += A[0]+A[s+1];
75+
cnt++;
76+
s++;
77+
}
78+
while(cnt<N-1) {
79+
ans += A[s]+A[e]-M;
80+
cnt++;
81+
if(A[s]+A[e-1] >= M) e--;
82+
else s++;
83+
}
84+
bw.write(ans + "\n");
85+
86+
87+
}
88+
89+
}
90+
91+
```

0 commit comments

Comments
 (0)