inlineintread() { int data = 0, w = 1; char ch = getchar(); while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if(ch == '-') w = -1, ch = getchar(); while(ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar(); return data*w; }
constintmaxn(10010); int n, P, k, ans, a[maxn], _max, d[maxn];
inlineboolcheck(int mid, int x) { for(RG int i = 1, w = 0, t, cnt = 1; i <= n; i++) { t = (a[i] + x) % P; if(mid < t) returnfalse; if(w + t > mid) ++cnt, w = 0; if(cnt > k) returnfalse; w += t; } returntrue; }
inlinevoidSolve(int x) { _max = 0; for(RG int i = 1; i <= n; i++) _max = max(_max, (a[i] + x) % P); int l = _max, r = 1e8 + 1, res = r; while(l <= r) { int mid = (l + r) >> 1; if(check(mid, x)) res = mid, r = mid - 1; else l = mid + 1; } ans = min(ans, res); }
intmain() { srand(time(NULL)); n = read(); P = read(); k = read(); for(RG int i = 1; i <= n; i++) a[i] = read(); for(RG int i = 1; i <= P; i++) d[i] = i - 1; ans = 1e8; random_shuffle(d + 1, d + P + 1); for(RG int i = 1; i <= P; i++) { if(!check(ans, d[i])) continue; Solve(d[i]); } printf("%d\n", ans); return0; }