Source Code
#include <bits/stdc++.h>
#define ll long long
#define pp push_back
#define ff first
#define ss second

using namespace std;

ll w,m,mm,h,r,l,ii,i1,jj,a,x[1000001],y[1000001],z,zz,g,aa,bb,b,c,d[100001],k,vis,kk,mod=1e9+7,q,inf=1e18;
ll T,i,j,n;
vector <ll> v[1000001],vv;
pair <ll,ll> p;
map <ll,ll> mp,mpp,mppp;
set <ll> see;
set <ll> se;
set <ll> :: iterator it,itt,it1;
string s,s2,s1,t;
char cc;

int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> x[i];
        a+=x[i];
    }
    for(i=0;i<n;i++)
    {
        cin >> y[i];
        b+=y[i];
    }
    if(a>b)
    {
        cout << -1;
        return 0;
    }
    z=a+n-1;
    m=1e18;
    a=0;
    b=0;
    for(i=0;i<n;i++)
    {
        a+=y[i];
        if(b>0)
            z+=2;
        if(x[i]>a)
        {
            m=min(m,z+n-i-1);
            b=x[i]-a;
            a=0;
        }
        else
            a-=x[i];
        if(a>=b)
        {
            a-=b;
            b=0;
        }
    }
    cout << min(m,z);
    return 0;
}
/*


*/
Copy
Shooting Balloons Khaled_Mardini
GNU G++17
59 ms
28.9 MB
Wrong Answer