#include <bits/stdc++.h>
#include "homecoming.h"
using namespace std;
const long long INF = 1e16;
const int MAX_N = 4e6 + 1;
deque <int> maxVal;
long long sumB[MAX_N], dp[MAX_N], val[MAX_N];
void add( int i ) {
while ( !maxVal.empty() && val[i] > val[maxVal.back()] )
maxVal.pop_back();
maxVal.push_back( i );
}
void rem( int i ) {
if ( !maxVal.empty() && maxVal.front() == i )
maxVal.pop_front();
}
long long getVal( int i ) {
if ( maxVal.empty() )
return -INF;
return dp[maxVal.front()] - (sumB[i + 1] - sumB[maxVal.front()]);
}
long long solve( int n, int k, int a[], int b[] ) {
long long ans = 0;
for ( int i = n; i <= n + k; i++ )
sumB[i] = 0;
for ( int i = n - 1; i >= 0; i-- )
sumB[i] = sumB[i + 1] + b[i];
for ( int take = 0; take <= 1; take++ ) {
long long s = 0;
for ( int i = 0; i <= k; i++ ) {
dp[n + i] = -s * (1 - take);
s += b[i];
}
maxVal.clear();
for ( int i = n + k - 1; i >= n; i-- ) {
val[i] = dp[i] + sumB[i];
add( i );
}
long long maxA = dp[n + k];
for ( int i = n - 1; i >= 0; i-- ) {
dp[i] = -INF;
if ( i >= k * take ) {
dp[i] = max( getVal( i ), maxA );
val[i] = dp[i] + sumB[i];
add( i );
}
rem( i + k );
maxA = max( maxA, dp[i + k] - (sumB[i + 1] - sumB[i + k]) );
maxA = maxA - b[i] + a[i];
}
ans = max( ans, getVal( -1 ) );
ans = max( ans, maxA );
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
15956 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
132 ms |
62936 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
7 ms |
1000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
32 ms |
15956 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
132 ms |
62936 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
7 ms |
1000 KB |
Output is correct |
16 |
Correct |
163 ms |
116248 KB |
Output is correct |
17 |
Correct |
65 ms |
32644 KB |
Output is correct |
18 |
Correct |
118 ms |
42644 KB |
Output is correct |
19 |
Correct |
114 ms |
60216 KB |
Output is correct |
20 |
Correct |
97 ms |
85416 KB |
Output is correct |
21 |
Correct |
111 ms |
58816 KB |
Output is correct |