Source Code
#include <bits/stdc++.h>
using namespace std;
    
int main (){

  #ifndef ONLINE_JUDGE
    freopen("SuhaibSawalha1","r",stdin);
  #endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  int a[n], b[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  long long rem = 0, sum = 0, ans = n - 1 + accumulate(a, a + n, 0LL);
  int last = 0;
  for (int i = 0; i < n; ++i) {
    sum += b[i];
    if (rem && sum >= rem) {
      ans += (i == n - 1 ? 1 : 2) * (i - last);
      sum -= rem;
      rem = 0;
    }
    if (rem) {
      rem += a[i];
    }
    else {
      if (sum >= a[i]) {
        sum -= a[i];
      }
      else {
        a[i] -= sum;
        rem = a[i];
        last = i;
      }
    }
  }
  cout << (rem ? -1 : ans);

  return 0;
}
Copy
Shooting Balloons SuhaibSawalha1
GNU G++17
0 ms
604 KB
Wrong Answer