#include <bits/stdc++.h>
#include "homecoming.h"
using namespace std;
const long long inf = 1e18;
long long solve(int n, int k, int *a, int *b)
{
vector <vector <long long> > dp(n+1);
for (int i = 1; i <= n; i++)
dp[i].resize(n+1);
vector <long long> pra(n+1, 0), prb(n+1, 0);
for (int i = 1; i <= n; i++)
{
pra[i] = pra[i-1] + a[i-1];
prb[i] = prb[i-1] + b[i-1];
}
for (int i = 1; i <= n; i++)
{
dp[i][i-1] = 0;
long long curr_max = -inf;
for (int j = i; j <= n; j++)
{
dp[i][j] = dp[i][j-1];
if (j - k + 1 < i)
continue;
int pos = j - k + 1;
curr_max = max(curr_max, prb[pos-1] - pra[pos-1] + dp[i][pos-1]);
dp[i][j] = max(dp[i][j], curr_max - prb[j] + pra[pos]);
}
}
long long bb = 0;
for (int i = 1; i <= n; i++)
{
bb -= b[i-1];
bb += a[i-1];
}
long long ans = max(bb, dp[1][n]);
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
long long curr = dp[i][j];
curr -= prb[i-1];
curr -= prb[n] - prb[j];
if (i-1 >= k)
curr += pra[i-k];
if (n-k+1 > j)
curr += pra[n-k+1] - pra[j];
int l = max(j+1, n - k + 2), r = min(n, i+n-k);
if (l <= r)
curr += pra[r] - pra[l-1];
ans = max(ans, curr);
}
}
return ans;
}
/*
const int maxn = 1e3 + 3;
int n, k;
int a[maxn], b[maxn];
int main()
{
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
cout << solve(n, k, a, b) << endl;
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
2284 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
2284 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
2284 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
2284 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
180 ms |
196404 KB |
Output is correct |
7 |
Correct |
180 ms |
196460 KB |
Output is correct |
8 |
Correct |
55 ms |
31920 KB |
Output is correct |
9 |
Correct |
203 ms |
196400 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
177 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
2284 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
3 ms |
2284 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
180 ms |
196404 KB |
Output is correct |
7 |
Correct |
180 ms |
196460 KB |
Output is correct |
8 |
Correct |
55 ms |
31920 KB |
Output is correct |
9 |
Correct |
203 ms |
196400 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Runtime error |
177 ms |
262144 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |