/* https://www.boi2022.de/wp-content/uploads/2022/05/boi2022-day1-vault-spoiler.pdf */
#include <stdio.h>
#include <string.h>
#define N 300
#define M (N * (N + 1))
#define INF 0x3f3f3f3f
long long min(long long a, long long b) { return a < b ? a : b; }
void update(int *dp, int m, int a, int k, int c) {
static int dq[M * 2 + 1], qu[M * 2 + 1];
int s, r, head, cnt;
if (a > 0) {
for (s = 0; s <= m; s++)
dq[s] = dp[s] == -INF ? -INF : dp[s] - s / a * c;
for (r = 0; r < a; r++) {
head = cnt = 0;
for (s = r; s <= m; s += a) {
if (dq[s] != -INF) {
while (cnt && dq[qu[head + cnt - 1]] < dq[s])
cnt--;
qu[head + cnt++] = s;
}
dp[s] = cnt == 0 ? -INF : dq[qu[head]] + s / a * c;
if (cnt && qu[head] == s - a * k)
head++, cnt--;
}
}
} else if (a < 0) {
a = -a;
for (s = 0; s <= m; s++)
dq[s] = dp[s] == -INF ? -INF : dp[s] + s / a * c;
for (r = 0; r < a; r++) {
head = cnt = 0;
for (s = (m - r) / a * a + r; s >= 0; s -= a) {
if (dq[s] != -INF) {
while (cnt && dq[qu[head + cnt - 1]] < dq[s])
cnt--;
qu[head + cnt++] = s;
}
dp[s] = cnt == 0 ? -INF : dq[qu[head]] - s / a * c;
if (cnt && qu[head] == s + a * k)
head++, cnt--;
}
}
} else {
if (c <= 0)
return;
for (s = 0; s <= m; s++)
if (dp[s] != -INF)
dp[s] += k * c;
}
}
int main() {
static long long kk[N * 2 + 1], ll[N * 2 + 1];
static int dp[M * 2 + 1];
int n, m, a, s;
long long sum_, sum, l, ans;
scanf("%d%lld", &n, &sum_), m = n * (n + 1);
sum = 0;
for (a = -n; a <= n; a++) {
scanf("%lld", &kk[n + a]);
ll[n + a] = kk[n + a], sum += ll[n + a] * a;
}
if (sum > sum_) {
for (a = n; a > 0 && sum > sum_; a--) {
l = min(ll[n + a], (sum - sum_ + a - 1) / a);
ll[n + a] -= l, sum -= l * a;
}
if (sum > sum_) {
printf("impossible\n");
return 0;
}
} else {
for (a = n; a > 0 && sum < sum_; a--) {
l = min(ll[n - a], (sum_ - sum + a - 1) / a);
ll[n - a] -= l, sum += l * a;
}
if (sum < sum_) {
printf("impossible\n");
return 0;
}
}
for (s = 0; s <= m * 2; s++)
dp[s] = -INF;
dp[m + 0] = 0;
for (a = -n; a <= n; a++) {
update(dp, m * 2 + 1, a, min(ll[n + a], n + 1), -1);
update(dp, m * 2 + 1, a, min(kk[n + a] - ll[n + a], n + 1), 1);
}
if (dp[m + sum_ - sum] == -INF) {
printf("impossible\n");
return 0;
}
ans = dp[m + sum_ - sum];
for (a = -n; a <= n; a++)
ans += ll[n + a];
printf("%lld\n", ans);
return 0;
}
Compilation message
vault.c: In function 'main':
vault.c:63:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d%lld", &n, &sum_), m = n * (n + 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
vault.c:66:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%lld", &kk[n + a]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
12 ms |
368 KB |
Output is correct |
7 |
Incorrect |
12 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
12 ms |
368 KB |
Output is correct |
7 |
Incorrect |
12 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
12 ms |
368 KB |
Output is correct |
7 |
Incorrect |
12 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
12 ms |
368 KB |
Output is correct |
7 |
Incorrect |
12 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
12 ms |
368 KB |
Output is correct |
7 |
Incorrect |
12 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |