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 int long long
int dp[3000010];
int a[10000],w[10000];
int siz;
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,q;cin>>n>>q;
for (int i=-n;i<=n;i++){
int k;cin>>k;
int num=1;
while (k>=num){
k-=num;
a[++siz]=num*i;
w[siz]=num;
num<<=1;
}
if (k>0){
a[++siz]=k*i;
w[siz]=k;
}
}
//for (int i=1;i<=siz;i++) cout<<a[i]<<' '<<w[i]<<"\n";
for (int i=0;i<=1010000;i++) dp[i]=-1e18;
dp[505000]=0;
for (int i=1;i<=siz;i++){
if (a[i]<0){
for (int j=0;j<=1010000;j++){
if (j-a[i]>=0 && j-a[i]<=1010000 && dp[j-a[i]]!=-1e18){
dp[j]=max(dp[j],dp[j-a[i]]+w[i]);
}
}
continue;
}
for (int j=1010000;j>=0;j--){
if (j-a[i]>=0 && j-a[i]<=1010000 && dp[j-a[i]]!=-1e18){
dp[j]=max(dp[j],dp[j-a[i]]+w[i]);
}
}
}
if (q>505000 || q<-505000 || dp[505000+q]==-1e18) cout<<"impossible\n";
else cout<<dp[505000+q]<<"\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... |