#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pb emplace_back
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
template<class T> void out(T a){cout<<a<<endl;}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
const ll inf=1001001001001001001;
int main(){
ll n,K;cin>>n>>K;
ll tot=0,sum=0;
vi a(n),b(n);
rep(i,n)cin>>a[i];
cin>>tot;
rep(i,n)cin>>b[i];
reverse(all(a));
rep(i,n)sum-=a[i]*(i+1);
rep(i,n)sum+=b[i]*(i+1);
rep(i,n)tot+=a[i];
rep(i,n)tot+=b[i];
sum-=K;
if(sum<0){
sum*=-1;swap(a,b);
}
auto sol=[&](vi v){
ll mx=1000000;
vi dp(mx+1,inf);dp[0]=0;
REP(i,1,v.size()+1){
vi ndp=dp;
rep(j,i){
deque<ll> deq;
for(ll k=j;k<=mx;k+=i){
if(dp[k]!=inf){
while(deq.size()&&dp[deq.back()]-deq.back()/i>=dp[k]-k/i)deq.pop_back();
deq.pb(k);
}
while(deq.size()&&deq.front()<k-i*v[i-1])deq.pop_front();
if(deq.size())chmin(ndp[k],dp[deq.front()]+(k-deq.front())/i);
}
}
dp=ndp;
}
return dp;
};
vi dpa=sol(a);
vi dpb=sol(b);
ll ans=inf;
rep(i,dpa.size())if(i+sum<dpb.size())chmin(ans,dpa[i]+dpb[i+sum]);
if(ans==inf)out("impossible");
else out(tot-ans);
}
Compilation message
vault.cpp: In function 'int main()':
vault.cpp:63:30: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | rep(i,dpa.size())if(i+sum<dpb.size())chmin(ans,dpa[i]+dpb[i+sum]);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
23768 KB |
Output is correct |
2 |
Correct |
48 ms |
23788 KB |
Output is correct |
3 |
Correct |
24 ms |
23816 KB |
Output is correct |
4 |
Correct |
121 ms |
23752 KB |
Output is correct |
5 |
Correct |
853 ms |
23780 KB |
Output is correct |
6 |
Correct |
858 ms |
23760 KB |
Output is correct |
7 |
Correct |
818 ms |
23780 KB |
Output is correct |
8 |
Correct |
808 ms |
23784 KB |
Output is correct |
9 |
Correct |
869 ms |
23788 KB |
Output is correct |
10 |
Correct |
812 ms |
23784 KB |
Output is correct |
11 |
Correct |
834 ms |
23776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
23768 KB |
Output is correct |
2 |
Correct |
48 ms |
23788 KB |
Output is correct |
3 |
Correct |
24 ms |
23816 KB |
Output is correct |
4 |
Correct |
121 ms |
23752 KB |
Output is correct |
5 |
Correct |
853 ms |
23780 KB |
Output is correct |
6 |
Correct |
858 ms |
23760 KB |
Output is correct |
7 |
Correct |
818 ms |
23780 KB |
Output is correct |
8 |
Correct |
808 ms |
23784 KB |
Output is correct |
9 |
Correct |
869 ms |
23788 KB |
Output is correct |
10 |
Correct |
812 ms |
23784 KB |
Output is correct |
11 |
Correct |
834 ms |
23776 KB |
Output is correct |
12 |
Correct |
32 ms |
23812 KB |
Output is correct |
13 |
Correct |
42 ms |
23752 KB |
Output is correct |
14 |
Correct |
24 ms |
23756 KB |
Output is correct |
15 |
Correct |
125 ms |
23756 KB |
Output is correct |
16 |
Correct |
867 ms |
23784 KB |
Output is correct |
17 |
Correct |
833 ms |
23792 KB |
Output is correct |
18 |
Correct |
905 ms |
23780 KB |
Output is correct |
19 |
Correct |
873 ms |
23788 KB |
Output is correct |
20 |
Correct |
849 ms |
23892 KB |
Output is correct |
21 |
Correct |
869 ms |
23776 KB |
Output is correct |
22 |
Correct |
840 ms |
23780 KB |
Output is correct |
23 |
Correct |
2047 ms |
23752 KB |
Output is correct |
24 |
Correct |
2094 ms |
23780 KB |
Output is correct |
25 |
Correct |
1880 ms |
23780 KB |
Output is correct |
26 |
Correct |
2457 ms |
23784 KB |
Output is correct |
27 |
Correct |
2433 ms |
23756 KB |
Output is correct |
28 |
Correct |
1775 ms |
23772 KB |
Output is correct |
29 |
Correct |
1755 ms |
23780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
23768 KB |
Output is correct |
2 |
Correct |
1073 ms |
24468 KB |
Output is correct |
3 |
Correct |
730 ms |
23972 KB |
Output is correct |
4 |
Incorrect |
1080 ms |
25836 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
23768 KB |
Output is correct |
2 |
Correct |
1073 ms |
24468 KB |
Output is correct |
3 |
Correct |
730 ms |
23972 KB |
Output is correct |
4 |
Incorrect |
1080 ms |
25836 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
23768 KB |
Output is correct |
2 |
Correct |
1073 ms |
24468 KB |
Output is correct |
3 |
Correct |
730 ms |
23972 KB |
Output is correct |
4 |
Incorrect |
1080 ms |
25836 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
23768 KB |
Output is correct |
2 |
Correct |
48 ms |
23788 KB |
Output is correct |
3 |
Correct |
24 ms |
23816 KB |
Output is correct |
4 |
Correct |
121 ms |
23752 KB |
Output is correct |
5 |
Correct |
853 ms |
23780 KB |
Output is correct |
6 |
Correct |
858 ms |
23760 KB |
Output is correct |
7 |
Correct |
818 ms |
23780 KB |
Output is correct |
8 |
Correct |
808 ms |
23784 KB |
Output is correct |
9 |
Correct |
869 ms |
23788 KB |
Output is correct |
10 |
Correct |
812 ms |
23784 KB |
Output is correct |
11 |
Correct |
834 ms |
23776 KB |
Output is correct |
12 |
Correct |
144 ms |
23768 KB |
Output is correct |
13 |
Correct |
1073 ms |
24468 KB |
Output is correct |
14 |
Correct |
730 ms |
23972 KB |
Output is correct |
15 |
Incorrect |
1080 ms |
25836 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
23768 KB |
Output is correct |
2 |
Correct |
1073 ms |
24468 KB |
Output is correct |
3 |
Correct |
730 ms |
23972 KB |
Output is correct |
4 |
Incorrect |
1080 ms |
25836 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
23768 KB |
Output is correct |
2 |
Correct |
48 ms |
23788 KB |
Output is correct |
3 |
Correct |
24 ms |
23816 KB |
Output is correct |
4 |
Correct |
121 ms |
23752 KB |
Output is correct |
5 |
Correct |
853 ms |
23780 KB |
Output is correct |
6 |
Correct |
858 ms |
23760 KB |
Output is correct |
7 |
Correct |
818 ms |
23780 KB |
Output is correct |
8 |
Correct |
808 ms |
23784 KB |
Output is correct |
9 |
Correct |
869 ms |
23788 KB |
Output is correct |
10 |
Correct |
812 ms |
23784 KB |
Output is correct |
11 |
Correct |
834 ms |
23776 KB |
Output is correct |
12 |
Correct |
32 ms |
23812 KB |
Output is correct |
13 |
Correct |
42 ms |
23752 KB |
Output is correct |
14 |
Correct |
24 ms |
23756 KB |
Output is correct |
15 |
Correct |
125 ms |
23756 KB |
Output is correct |
16 |
Correct |
867 ms |
23784 KB |
Output is correct |
17 |
Correct |
833 ms |
23792 KB |
Output is correct |
18 |
Correct |
905 ms |
23780 KB |
Output is correct |
19 |
Correct |
873 ms |
23788 KB |
Output is correct |
20 |
Correct |
849 ms |
23892 KB |
Output is correct |
21 |
Correct |
869 ms |
23776 KB |
Output is correct |
22 |
Correct |
840 ms |
23780 KB |
Output is correct |
23 |
Correct |
2047 ms |
23752 KB |
Output is correct |
24 |
Correct |
2094 ms |
23780 KB |
Output is correct |
25 |
Correct |
1880 ms |
23780 KB |
Output is correct |
26 |
Correct |
2457 ms |
23784 KB |
Output is correct |
27 |
Correct |
2433 ms |
23756 KB |
Output is correct |
28 |
Correct |
1775 ms |
23772 KB |
Output is correct |
29 |
Correct |
1755 ms |
23780 KB |
Output is correct |
30 |
Correct |
144 ms |
23768 KB |
Output is correct |
31 |
Correct |
1073 ms |
24468 KB |
Output is correct |
32 |
Correct |
730 ms |
23972 KB |
Output is correct |
33 |
Incorrect |
1080 ms |
25836 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
23768 KB |
Output is correct |
2 |
Correct |
1073 ms |
24468 KB |
Output is correct |
3 |
Correct |
730 ms |
23972 KB |
Output is correct |
4 |
Incorrect |
1080 ms |
25836 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
23768 KB |
Output is correct |
2 |
Correct |
48 ms |
23788 KB |
Output is correct |
3 |
Correct |
24 ms |
23816 KB |
Output is correct |
4 |
Correct |
121 ms |
23752 KB |
Output is correct |
5 |
Correct |
853 ms |
23780 KB |
Output is correct |
6 |
Correct |
858 ms |
23760 KB |
Output is correct |
7 |
Correct |
818 ms |
23780 KB |
Output is correct |
8 |
Correct |
808 ms |
23784 KB |
Output is correct |
9 |
Correct |
869 ms |
23788 KB |
Output is correct |
10 |
Correct |
812 ms |
23784 KB |
Output is correct |
11 |
Correct |
834 ms |
23776 KB |
Output is correct |
12 |
Correct |
32 ms |
23812 KB |
Output is correct |
13 |
Correct |
42 ms |
23752 KB |
Output is correct |
14 |
Correct |
24 ms |
23756 KB |
Output is correct |
15 |
Correct |
125 ms |
23756 KB |
Output is correct |
16 |
Correct |
867 ms |
23784 KB |
Output is correct |
17 |
Correct |
833 ms |
23792 KB |
Output is correct |
18 |
Correct |
905 ms |
23780 KB |
Output is correct |
19 |
Correct |
873 ms |
23788 KB |
Output is correct |
20 |
Correct |
849 ms |
23892 KB |
Output is correct |
21 |
Correct |
869 ms |
23776 KB |
Output is correct |
22 |
Correct |
840 ms |
23780 KB |
Output is correct |
23 |
Correct |
2047 ms |
23752 KB |
Output is correct |
24 |
Correct |
2094 ms |
23780 KB |
Output is correct |
25 |
Correct |
1880 ms |
23780 KB |
Output is correct |
26 |
Correct |
2457 ms |
23784 KB |
Output is correct |
27 |
Correct |
2433 ms |
23756 KB |
Output is correct |
28 |
Correct |
1775 ms |
23772 KB |
Output is correct |
29 |
Correct |
1755 ms |
23780 KB |
Output is correct |
30 |
Correct |
144 ms |
23768 KB |
Output is correct |
31 |
Correct |
1073 ms |
24468 KB |
Output is correct |
32 |
Correct |
730 ms |
23972 KB |
Output is correct |
33 |
Incorrect |
1080 ms |
25836 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |