USACO Ski Course Design

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
40
41
42
43
44
45
46
/*
ID: wcr19961
PROG: skidesign
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <set>
using namespace std;

typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 110;
int a[maxn];

int main() {
freopen("skidesign.in", "r", stdin);
freopen("skidesign.out", "w", stdout);
int n, ans = inf, l = 100, r = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
l = min(l, x), r = max(r, x);
++a[x];
}
for (int i = l; i <= r; ++i) {
int res = 0;
for (int j = l; j <= r; ++j) {
if (a[j] > 0) {
if (j < i) {
res += a[j] * (j - i) * (j - i);
}
if (j > i + 17) {
res += a[j] * (j - i - 17) * (j - i - 17);
}
}
}
ans = min(ans, res);
}
printf("%d\n", ans);
return 0;
}