#include #include #include #include int primerjaj_cela (const void * a, const void * b) { const int * c = (const int *) a; const int * d = (const int *) b; return *d - *c; /* najv proti najm */ } int main (int argc, char ** argv) { char * buf = malloc(500); fgets(buf, 500, stdin); char * cp; int N = strtoll(buf, &cp, 10); cp++; int M = strtoll(cp, NULL, 10); int * x = calloc(N+1, sizeof(int)); int * g = malloc((M+1)*sizeof(int)); int * gl = malloc((M+1)*sizeof(int)); for (int o = 0; o < M; o++) g[o] = INT_MAX; realloc(buf, (N+1)*6*9); fgets(buf, (N+1)*6*9, stdin); cp = buf; int i = 0; int mx = 0; do { int xi = strtoll(cp, &cp, 10); // if (xi != 0) x[++i] = xi; if (xi > mx) mx = xi; cp++; } while (cp[-1] != 0 && cp[-1] != '\n' && cp[-1] != '\r'); free(buf); if (N != i) { fprintf(stderr, "error %d ni %d\n", N, i); for (int o = 1; o <= i; o++) fprintf(stderr, " %d\n", x[o]); } fprintf(stderr, "max = %d\n", mx); for (i = 0; i <= mx; i++) { int s = 0; for (int j = 1; j <= N; j++) s += abs(i-x[j]); int mo = 0; int me = 0; for (int o = 0; o < M; o++) { if (g[o] > mo) { mo = g[o]; me = o; } } if (mo > s) g[me] = s; } int s = 0; for (int o = 0; o < M; o++) { fprintf(stderr, "GOSTILNA %d ima seštevek %d\n", o, g[o]); s += g[o]; } fprintf(stdout, "%d\n", s); return 0; }