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;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e18+7;
const ll LIM=1e6+7;
vector<int>V[LIM];
ll dp[LIM], dp2[LIM], A[LIM], B[LIM], l[LIM], sz[LIM];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
ll n, m;
cin >> n >> m;
rep(i, n) cin >> A[n-i];
rep(i, n+1) cin >> B[i];
ll ans=0;
rep(i, n+1) ans+=B[i];
rep(i, n+1) ans+=A[i];
rep(i, n+1) m+=A[i]*(ll)i;
rep(i, n+1) m-=B[i]*(ll)i;
if(m<0) {
m*=-1;
rep(i, n+1) swap(A[i], B[i]);
}
rep(i, LIM) dp[i]=INF;
dp[0]=0;
int ile=1;
for(ll i=n; i; --i) {
if(m>=LIM/2) {
ll p=m-LIM/2;
p=(p+i-1)/i;
p=min(p, max(A[i]-300/ile, 0ll));
A[i]-=p;
if(A[i]) ++ile;
dp[0]+=p;
m-=p*i;
}
}
if(m>=LIM) {
cout << "impossible\n";
return 0;
}
for(int i=1; i<=n; ++i) rep(j, LIM/i+1) V[i-1].pb(0);
rep(xd, 2) {
for(ll i=1; i<=n; ++i) if(A[i]) {
rep(j, i) l[j]=sz[j]=0;
rep(j, LIM) {
ll p=j%i;
while(l[p]<sz[p] && (j-V[p][l[p]])/i>A[i]) ++l[p];
while(sz[p]>0 && dp[j]<=dp[V[p][sz[p]-1]]+(j-V[p][sz[p]-1])/i) {
--sz[p];
l[p]=min(l[p], sz[p]);
}
dp2[j]=dp[j];
if(l[p]<sz[p]) dp2[j]=min(dp2[j], dp[V[p][l[p]]]+(j-V[p][l[p]])/i);
V[p][sz[p]]=j;
++sz[p];
}
rep(j, LIM) dp[j]=dp2[j];
}
rep(i, LIM/2) swap(dp[i], dp[LIM-i-1]);
rep(i, n+1) swap(A[i], B[i]);
}
if(dp[m]==INF) {
cout << "impossible\n";
return 0;
}
cout << ans-dp[m] << '\n';
}
# | 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... |