Source Code
#include "bits/stdc++.h"


#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O2")


using namespace std;

#define endl            "\n"
#define all(v)          v.begin(), v.end()
#define debug(x)        cout << #x << " is " << x << endl
#define isOdd(n)        (n&1)
#define pow(n, m)       (ll)powl(n, m)
#define Unique(x)       x.erase(unique(all(x)), x.end())
#define clr(x, val)    memset(x, val, sizeof(x))
#define numOfDigits(x)  (x ? (ll)(log10(x)) + 1 : 1)


using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;
using tii = tuple<int, int, int>;


int main()
{

    ios_base::sync_with_stdio(0), cin.tie(0);

    int n; cin >> n;
    ll sum1 = 0, sum2 = 0;
    int a[n], b[n];
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum1 += a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
        sum2 += b[i];
    }
    if(sum2 < sum1) return cout << -1, 0;

    stack<int> s;
    ll bullets = 0, steps = 0, current = 0;
    for (int i = 0; i < n; ++i) {
        bullets += b[i];
        s.push(i);
        while (s.size() && bullets >= a[s.top()]) {
            bullets -= a[s.top()];
            steps += abs(current - s.top()) + a[s.top()];
            current = s.top();
            s.pop();
        }
    }
    while (s.size() && bullets >= a[s.top()]) {
        bullets -= a[s.top()], steps += abs(current - s.top());
        current = s.top();
        s.pop();
    }
    cout << steps;
    return 0;
}   
Copy
Shooting Balloons Moustafa
GNU G++17
0 ms
680 KB
Wrong Answer