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
61
62
63
64
65
66
67
68
69
70
71
72
73
|
void solve() {
ll k, n, p, q;
cin >> n >> k >> p >> q;
if (p < q) swap(p, q);
vl a(n);
rep(i, 0, n - 1) cin >> a[i];
if (p == q) {
ll ans = 0;
rep(i, 0, n - 1) ans += a[i] % p;
cout << ans << endl;
return;
}
vi tem(n);
ll ans = 0;
rep(i, 0, n - 1) {
int tem2 = (a[i] % p) % q;
int tem3 = (a[i] % q);
if (tem2 < tem3)
tem[i] = 1;
else if (tem2 > tem3)
tem[i] = 0;
else
tem[i] = 2;
ans += 1LL * min(tem2, tem3);
}
int las = -1;
int tem3 = 0;
vi tem2;
rep(i, 0, n - 1) {
if (tem[i] == 0) {
if (i != 0 && (tem[i - 1] == 1 || tem[i - 1] == 2)) {
tem2.push_back(tem3);
tem3 = 0;
}
} else
tem3++;
}
if (tem3 != 0) tem2.push_back(tem3);
if (!tem2.empty() && *max_element(all(tem2)) >= k) {
cout << ans << endl;
return;
}
tem2.clear();
tem3 = 0;
rep(i, 0, n - 1) {
if (tem[i] == 1) {
if (i != 0 && (tem[i - 1] == 0 || tem[i - 1] == 2)) {
tem2.push_back(tem3);
tem3 = 0;
}
} else
tem3++;
}
if (tem3 != 0) tem2.push_back(tem3);
if (!tem2.empty() && *max_element(all(tem2)) >= k) {
cout << ans << endl;
return;
}
ll cnt0 = 0, cnt1 = 0, cnt2 = 0;
vl tem4;
rep(i, 0, n - 1) {
cnt0 += (a[i] % p) % q;
cnt1 += a[i] % q;
cnt2 += min((a[i] % p) % q, a[i] % q);
if (i < k - 1) continue;
tem4.push_back(ans + min(cnt0, cnt1) - cnt2);
cnt0 -= a[i - k + 1] % p % q;
cnt1 -= a[i - k + 1] % q;
cnt2 -= min(a[i - k + 1] % p % q, a[i - k + 1] % q);
}
cout << *min_element(all(tem4)) << endl;
return;
}
|