USACO Barn Repair

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
ID: wcr19961
PROG: barn1
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 210;
int a[maxn], tmp[maxn];

int main() {
freopen("barn1.in", "r", stdin);
freopen("barn1.out", "w", stdout);
int m, s, c;
scanf("%d%d%d", &m, &s, &c);
for (int i = 0; i < c; ++i) {
scanf("%d", &a[i]);
}
sort(a, a + c);
int cnt = 0;
for (int i = 1; i < c; ++i) {
tmp[cnt++] = a[i] - a[i - 1] - 1;
}
sort(tmp, tmp + cnt);
int ans = a[c - 1] - a[0] + 1;
--m;
while (m && cnt) {
ans -= tmp[--cnt];
--m;
}
printf("%d\n", ans);
return 0;
}