# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
716747 | browntoad | Stove (JOI18_stove) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.util.*;
public class stove
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int n, k;
n = obj.nextInt();
k = obj.nextInt();
int arr[] = new int [n];
for (int i=0; i<n; i++){
arr[i] = obj.nextInt();
}
int ans = n;
if (n == 1){
System.out.println(ans);
return;
}
int sarr[] = new int[n-1];
for (int i=0; i<n-1; i++){
sarr[i] = arr[i+1]-arr[i]-1;
}
for (int i=0; i<n-2; i++){
int id = i;
for (int j=i+1; j<n-1; j++){
if (sarr[j]<sarr[id]){
id=j;
}
}
int tmp = sarr[id];
sarr[id] = sarr[i];
sarr[i] = tmp;
}
for(int i=0; i<n-k; i++){
ans += sarr[i];
}
System.out.println(ans);
}
}