#include <stdio.h>
#define ll long long
const int N=2000050;
const int M=2*N;
const ll inf=9e18;
ll max(ll a, ll b){ return a>b?a:b;}
ll sum[M],a[N],dp[N][2],b[M];
ll Get(int l, int r){ return sum[r]-sum[l-1];}
ll solve(int n, int k, int *A, int *B)
{
int i,j;
ll sol=0;
for(i=1;i<=n;i++) a[i]=A[i-1],b[i]=-B[i-1];
for(i=n+1;i<=n*2;i++) b[i]=b[i-n];
for(i=1;i<=2*n;i++) sum[i]=sum[i-1]+b[i];
dp[1][0]=0;
dp[1][1]=-inf;
for(i=2;i<=n;i++)
{
dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
dp[i][1]=max(dp[i-1][0]+Get(i,i+k-1)+a[i],dp[i-1][1]+a[i]+b[i+k-1]);
}
sol=max(sol,max(dp[n][0],dp[n][1]));
dp[1][0]=-inf;
dp[1][1]=a[1]+Get(1,k);
for(i=2;i<=n;i++)
{
dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
if(i+k-1<=n) dp[i][1]=max(dp[i-1][0]+Get(i,i+k-1)+a[i],dp[i-1][1]+a[i]+b[i+k-1]);
else dp[i][1]=max(dp[i-1][0]+Get(i,n)+a[i],dp[i-1][1]+a[i]);
}
sol=max(sol,max(dp[n][0],dp[n][1]));
return sol;
}
Compilation message
homecoming.cpp: In function 'long long int solve(int, int, int*, int*)':
homecoming.cpp:11:8: warning: unused variable 'j' [-Wunused-variable]
int i,j;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
3 ms |
536 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
3 ms |
536 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
6 |
Correct |
3 ms |
1048 KB |
Output is correct |
7 |
Correct |
3 ms |
1048 KB |
Output is correct |
8 |
Correct |
3 ms |
1048 KB |
Output is correct |
9 |
Correct |
4 ms |
1048 KB |
Output is correct |
10 |
Correct |
4 ms |
1048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
31824 KB |
Output is correct |
2 |
Correct |
6 ms |
31824 KB |
Output is correct |
3 |
Correct |
312 ms |
125792 KB |
Output is correct |
4 |
Correct |
5 ms |
125792 KB |
Output is correct |
5 |
Correct |
17 ms |
125792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
3 ms |
536 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
6 |
Correct |
3 ms |
1048 KB |
Output is correct |
7 |
Correct |
3 ms |
1048 KB |
Output is correct |
8 |
Correct |
3 ms |
1048 KB |
Output is correct |
9 |
Correct |
4 ms |
1048 KB |
Output is correct |
10 |
Correct |
4 ms |
1048 KB |
Output is correct |
11 |
Correct |
67 ms |
31824 KB |
Output is correct |
12 |
Correct |
6 ms |
31824 KB |
Output is correct |
13 |
Correct |
312 ms |
125792 KB |
Output is correct |
14 |
Correct |
5 ms |
125792 KB |
Output is correct |
15 |
Correct |
17 ms |
125792 KB |
Output is correct |
16 |
Correct |
276 ms |
126028 KB |
Output is correct |
17 |
Correct |
107 ms |
126028 KB |
Output is correct |
18 |
Correct |
179 ms |
126028 KB |
Output is correct |
19 |
Correct |
229 ms |
126028 KB |
Output is correct |
20 |
Correct |
291 ms |
135064 KB |
Output is correct |
21 |
Correct |
189 ms |
135064 KB |
Output is correct |