import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStream;
public class stove {
private static class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int getInt() {
return Integer.parseInt(nextToken());
}
public double getDouble() {
return Double.parseDouble(nextToken());
}
public long getLong() {
return Long.parseLong(nextToken());
}
public String getWord() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) {
}
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
public static void main(String[] args) {
try(Kattio io = new Kattio(System.in, System.out)) {
int n = io.getInt();
int k = io.getInt();
if (k == n) {
io.println(n);
return;
}
Integer[] gaps = new Integer[n-1];
int prevInt = -1;
int firstInt = -1;
for (int i=0; i<n; ++i) {
int curInt = io.getInt();
if (i>0) {
gaps[i-1] = curInt - prevInt - 1;
} else {
firstInt = curInt;
}
prevInt = curInt;
}
Arrays.sort(gaps, Collections.reverseOrder());
int result = prevInt + 1 - firstInt;
for (int i=0; i<k-1; ++i) {
result -= gaps[i];
}
io.println(result);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
9300 KB |
Output is correct |
2 |
Correct |
43 ms |
8888 KB |
Output is correct |
3 |
Correct |
43 ms |
9068 KB |
Output is correct |
4 |
Correct |
43 ms |
9256 KB |
Output is correct |
5 |
Correct |
44 ms |
9344 KB |
Output is correct |
6 |
Correct |
49 ms |
8840 KB |
Output is correct |
7 |
Correct |
43 ms |
8948 KB |
Output is correct |
8 |
Correct |
44 ms |
9240 KB |
Output is correct |
9 |
Correct |
43 ms |
9064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
9300 KB |
Output is correct |
2 |
Correct |
43 ms |
8888 KB |
Output is correct |
3 |
Correct |
43 ms |
9068 KB |
Output is correct |
4 |
Correct |
43 ms |
9256 KB |
Output is correct |
5 |
Correct |
44 ms |
9344 KB |
Output is correct |
6 |
Correct |
49 ms |
8840 KB |
Output is correct |
7 |
Correct |
43 ms |
8948 KB |
Output is correct |
8 |
Correct |
44 ms |
9240 KB |
Output is correct |
9 |
Correct |
43 ms |
9064 KB |
Output is correct |
10 |
Correct |
154 ms |
13236 KB |
Output is correct |
11 |
Correct |
145 ms |
12716 KB |
Output is correct |
12 |
Correct |
164 ms |
12548 KB |
Output is correct |
13 |
Correct |
151 ms |
12360 KB |
Output is correct |
14 |
Correct |
176 ms |
12840 KB |
Output is correct |
15 |
Correct |
125 ms |
11816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
9300 KB |
Output is correct |
2 |
Correct |
43 ms |
8888 KB |
Output is correct |
3 |
Correct |
43 ms |
9068 KB |
Output is correct |
4 |
Correct |
43 ms |
9256 KB |
Output is correct |
5 |
Correct |
44 ms |
9344 KB |
Output is correct |
6 |
Correct |
49 ms |
8840 KB |
Output is correct |
7 |
Correct |
43 ms |
8948 KB |
Output is correct |
8 |
Correct |
44 ms |
9240 KB |
Output is correct |
9 |
Correct |
43 ms |
9064 KB |
Output is correct |
10 |
Correct |
154 ms |
13236 KB |
Output is correct |
11 |
Correct |
145 ms |
12716 KB |
Output is correct |
12 |
Correct |
164 ms |
12548 KB |
Output is correct |
13 |
Correct |
151 ms |
12360 KB |
Output is correct |
14 |
Correct |
176 ms |
12840 KB |
Output is correct |
15 |
Correct |
125 ms |
11816 KB |
Output is correct |
16 |
Correct |
584 ms |
19324 KB |
Output is correct |
17 |
Correct |
570 ms |
18964 KB |
Output is correct |
18 |
Correct |
631 ms |
18996 KB |
Output is correct |
19 |
Correct |
581 ms |
18676 KB |
Output is correct |
20 |
Correct |
582 ms |
18760 KB |
Output is correct |
21 |
Correct |
623 ms |
19052 KB |
Output is correct |
22 |
Correct |
575 ms |
18860 KB |
Output is correct |
23 |
Correct |
586 ms |
18792 KB |
Output is correct |
24 |
Correct |
662 ms |
19408 KB |
Output is correct |