This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define rng(i,l,r) for(ll i=(l); i<(r); i++)
#define all(x) x.begin(),x.end()
using ll=long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll M,L;
cin>>M>>L;
vector<ll> A(M*2+1);
rep(i,M*2+1) {
cin>>A[i];
}
if(L<0) {
L=-L;
rep(i,M) {
swap(A[i],A[M*2-i]);
}
}
const ll mx=510000;
vector<vector<ll>> dp(M+2,vector<ll>(mx+1,-(1LL<<60)));
dp[M+1][0]=0;
for(ll i=M; i>0; i--) {
rep(j,i) {
ll cnt=0;
deque<pair<ll,ll>> deq;
for(ll k=j; k<=mx; k+=i) {
while(!deq.empty()&&deq.front().first<=dp[i+1][k]-cnt) {
deq.pop_front();
}
deq.push_front({dp[i+1][k]-cnt,cnt});
if(deq.back().second<cnt-A[M+i])deq.pop_back();
dp[i][k]=deq.back().first+cnt;
cnt++;
}
}
}
vector<vector<ll>> dp2(M+2,vector<ll>(mx+1,-(1LL<<60)));
dp2[M+1][0]=0;
for(ll i=M; i>0; i--) {
rep(j,i) {
ll cnt=0;
deque<pair<ll,ll>> deq;
for(ll k=j; k<=mx; k+=i) {
while(!deq.empty()&&deq.front().first<=dp2[i+1][k]-cnt) {
deq.pop_front();
}
deq.push_front({dp2[i+1][k]-cnt,cnt});
if(deq.back().second<cnt-A[M-i])deq.pop_back();
dp2[i][k]=deq.back().first+cnt;
cnt++;
}
}
}
ll ans=-(1LL<<60);
rng(i,L,mx) {
ans=max(ans,dp[1][i]+dp2[1][i-L]+A[M]);
}
if(ans<0)cout<<"impossible\n";
else cout<<ans<<"\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |