QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#657406 | #8058. Binary vs Ternary | ucup-team4992# | RE | 0ms | 0kb | C++20 | 1.9kb | 2024-10-19 14:42:58 | 2024-10-19 14:42:59 |
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int M=5e5+5;
ll A[M],B[M],C[M],D[M];
int m;
void solve(){
cin>>m;
for(int i=0; i<m; i++) cin>>A[i],C[i]=A[i];
for(int i=0; i<m; i++) cin>>B[i],D[i]=B[i];
A[m]=B[m]=C[m]=D[m]=0;
ll ans=0;
for(int i=0; i<m; i++){
ll a=A[i],b=B[m-1-i];
if(i==0) a=2e9;
if(i==m-1) b=2e9;
// cout<<"i = "<<i<<" , a = "<<a<<" , m-1-i = "<<m-1-i<<" , b = "<<b<<"\n";
ll res=min(a,b);
A[i]-=res; B[m-1-i]-=res;
ans+=res;
}
bool flag=0,can=0;
for(int i=1; i<m; i++){
flag|=(B[m-i]>0);
if(flag && A[i]>0){
can=1;
break;
}
}
if(can){
ll tot=0;
for(int i=1; i<m; i++){
tot+=B[m-i];
ll res=min(tot,A[i]);
tot-=res;
ans+=res;
}
cout<<ans<<"\n";
}else{
flag=0,can=0;
for(int i=1; i<m; i++){
flag|=(B[m-i]>0);
if(flag && C[i]>0){
can=1;
break;
}
}
if(can){
cout<<ans<<"\n";
return;
}
flag=0,can=0;
for(int i=1; i<m; i++){
flag|=(A[m-i]>0);
if(flag && D[i]>0){
can=1;
break;
}
}
if(can){
cout<<ans<<"\n";
return;
}
int cnt=0;
for(int i=0; i<m; i++){
ll a=C[i],b=D[m-1-i];
if(i==0) a=2e9;
if(i==m-1) b=2e9;
if(a&&b) cnt++;
}
if(cnt<=1){
cout<<"0\n";
return;
}
cout<<max(ans-1,0ll)<<"\n";
}
return;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T=1;
cin>>T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
3 1 111 110110 1101010 1111 111111