#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "homecoming.h"
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
ll prefa[4222222];
ll prefb[4222222];
ll suma(int l, int r)
{
if(l==0) return prefa[r];
else return prefa[r]-prefa[l-1];
}
ll sumb(int l, int r)
{
if(l>r) return 0;
if(l==0) return prefb[r];
else return prefb[r]-prefb[l-1];
}
const ll INF=ll(1e18);
ll solve(int n, int k, int *a, int *b)
{
ll ans = 0;
for(int i=0;i<2*n;i++)
{
prefa[i]=a[i%n]; prefb[i]=b[i%n];
if(i>0)
{
prefa[i]+=prefa[i-1]; prefb[i]+=prefb[i-1];
}
}
ans = max(0LL,prefa[n-1]-prefb[n-1]);
if(k==n) return ans;
vector<vector<ll> > dp[2];
for(int j=0;j<2;j++)
{
dp[j].resize(k+1);
for(int i=0;i<=k;i++)
{
dp[j][i].assign(n,-INF);
}
}
for(int s=0;s<=k;s++)
{
//s must be a starting point
dp[1][s][s] = a[s] - sumb(s,s+k-1);
ans=max(ans,dp[1][s][s]);
if(s==k)
{
dp[0][s][s]=0;
}
for(int i=s+1;i<n;i++)
{
dp[0][s][i] = max(dp[0][s][i-1],dp[0][s][i]);
dp[1][s][i] = max(dp[0][s][i-1] + a[i] - sumb(i, min(n+s-1, i+k-1)), dp[1][s][i-1] + a[i] - (i+k-1>=n+s?0:b[(i+k-1)%n]));
//cerr<<0<<' '<<s<<' '<<i<<' '<<dp[0][s][i]<<'\n';
//cerr<<1<<' '<<s<<' '<<i<<' '<<dp[1][s][i]<<'\n';
ans=max(ans,dp[0][s][i]);
ans=max(ans,dp[1][s][i]);
}
}
return ans;
}
/*
int aaaa[2111111];
int bbbb[2111111];
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>aaaa[i];
}
for(int i=0;i<n;i++)
{
cin>>bbbb[i];
}
cout<<solve(n,k,aaaa,bbbb)<<'\n';
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
3688 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
3688 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
135 ms |
59296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
3688 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3688 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |