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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/*
ID: mytbk921
LANG: C
TASK: skidesign
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
}
int calc_sum(int min, int N, int a[])
{
int sum = 0;
int i;
for (i = 0; i < N && a[i] < min; i++) {
int diff = min - a[i];
sum += diff * diff;
}
for (i = N-1; i >= 0 && a[i] - min > 17; i--) {
int diff = a[i] - min - 17;
sum += diff * diff;
}
return sum;
}
int main()
{
FILE *fin, *fout;
int N;
int m[1000];
int sum, ans;
int i;
fin = fopen("skidesign.in", "r");
fout = fopen("skidesign.out", "w");
fscanf(fin, "%d", &N);
for (i = 0; i<N; i++)
fscanf(fin, "%d", &m[i]);
fclose(fin);
qsort(m, N, sizeof(int), compare);
ans = calc_sum(m[0], N, m);
for (i = m[N-1]; i > 0; i--) {
sum = calc_sum(m[0]+i, N, m);
if (sum < ans)
ans = sum;
}
fprintf(fout, "%d\n", ans);
fclose(fout);
return 0;
}
|