#include "homecoming.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;
const int len = 2e6+5;
const ll inf = 1e17;
int ar[len], br[len], n, k;
ll suf[len], sufa[len], sufb[len], mx[len], dp[len];
deque<pair<int, ll> > deq;
ll fix(int st){
for (int i = n-1; i >= 0; i--){
sufa[i] = ar[(st+i)%n] + sufa[i+1];
sufb[i] = br[(st+i)%n] + sufb[i+1];
}
mx[n] = dp[n] = suf[n] = 0;
for (int i = n-1; i >= 0; i--){
dp[i] = sufa[i]-sufb[i] + mx[i+1];
//for (int j = i+1; j <= n+1; j++)
// dp[i] = max(dp[i], sufa[i]-sufb[i] + sufb[j+k-1]-sufa[j]+suf[j]);
suf[i] = max(suf[i+1], dp[i]);
mx[i] = max(mx[i+1], sufb[i+k-1]-sufa[i]+suf[i]);
}
return dp[0];
}
void init(){
deq.clear();
for (int i = 0; i <= 2*n; i++)
sufa[i] = sufb[i] = 0;
}
ll solve(int N, int K, int A[], int B[]){
n = N, k = K;
for (int i = 0; i < n; i++){
ar[i] = A[i];
br[i] = B[i];
}
init();
int pos = 0;
ll best = -inf;
for (int i = 2*n-1; i >= 0; i--){
sufa[i] = sufa[i+1] + ar[i%n];
sufb[i] = sufb[i+1] + br[i%n];
}
for (int i = 0, j = 1; i < n; i++){
while (!deq.empty() && deq.front().fi <= i)
deq.pop_front();
while (j-i <= n-k+1){
ll val = sufb[j+k-1]-sufa[j];
while (!deq.empty() && val > deq.back().se)
deq.pop_back();
deq.push_back(mp(j, val));
j++;
}
if (sufa[i]-sufb[i] + deq.front().se > best)
best = sufa[i]-sufb[i] + deq.front().se, pos = i;
}
init();
return max(0LL, fix(pos));
}
/*
1
3 2
40 80 100
140 0 20
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
888 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
888 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
37572 KB |
Output is correct |
2 |
Correct |
5 ms |
1016 KB |
Output is correct |
3 |
Correct |
317 ms |
115696 KB |
Output is correct |
4 |
Correct |
5 ms |
2040 KB |
Output is correct |
5 |
Correct |
16 ms |
4068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
888 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
888 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
89 ms |
37572 KB |
Output is correct |
12 |
Correct |
5 ms |
1016 KB |
Output is correct |
13 |
Correct |
317 ms |
115696 KB |
Output is correct |
14 |
Correct |
5 ms |
2040 KB |
Output is correct |
15 |
Correct |
16 ms |
4068 KB |
Output is correct |
16 |
Incorrect |
324 ms |
116664 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |