Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rg register
#define min(a, b) (a) < (b) ? (a) : (b)
#define max(a, b) (a) > (b) ? (a) : (b)
#define abs(a) ((a) > 0 ? (a) : -(a))
#define ll long long
#define maxn 50000
#pragma GCC optimize(3)
#pragma - fcrossjumping
#pragma - fdefer - pop
#pragma - fmerge - constans
#pragma - fthread - jumps
#pragma - floop - optimize
#pragma - fif - conversion
#pragma - fif - conversion2
#pragma - fdelayed - branch
#pragma - fguess - branch - probability
#pragma - fcprop - registers
#pragma - fforce - mem
#pragma - foptimize - sibling - calls
#pragma - fstrength - reduce
#pragma - fgcse
#pragma - fcse - follow - jumps
#pragma - frerun - cse - after - loop
#pragma - fdelete - null - pointer - checks
#pragma - fextensive - optimizations
#pragma - fregmove
#pragma - fschedule - insns
#pragma - fsched - interblock
#pragma - fcaller - saves
#pragma - fpeephole2
#pragma - freorder - blocks
#pragma - fstrict - aliasing
#pragma - funit - at - a - time
#pragma - falign - functions
#pragma - fcrossjumping
#pragma - finline - functions
#pragma - fweb
using namespace std;
inline ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
ll n;
ll a[maxn];
ll b[maxn];
ll ans;
int main() {
  n = read();
  for (rg int i = 1; i <= n + 1; ++i)
    a[i] = read();
  for (rg int i = 1; i <= n; ++i)
    b[i] = read();
  for (rg int i = n + 1; i >= 1; --i) {
    if (b[i] >= a[i]) {
      ans += a[i];
      b[i] -= a[i];
      a[i] = 0;
    } else {
      a[i] -= b[i];
      ans += b[i];
      b[i] = 0;
      if (b[i - 1] >= a[i]) {
        ans += a[i];

        b[i - 1] -= a[i];
        a[i] = 0;
      } else {

        a[i] -= b[i - 1];
        ans += b[i - 1];
        b[i - 1] = 0;
      }
    }
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rg register
#define min(a, b) (a) < (b) ? (a) : (b)
#define max(a, b) (a) > (b) ? (a) : (b)
#define abs(a) ((a) > 0 ? (a) : -(a))
#define ll long long
#define maxn 250000
#pragma GCC optimize(3)
#pragma - fcrossjumping
#pragma - fdefer - pop
#pragma - fmerge - constans
#pragma - fthread - jumps
#pragma - floop - optimize
#pragma - fif - conversion
#pragma - fif - conversion2
#pragma - fdelayed - branch
#pragma - fguess - branch - probability
#pragma - fcprop - registers
#pragma - fforce - mem
#pragma - foptimize - sibling - calls
#pragma - fstrength - reduce
#pragma - fgcse
#pragma - fcse - follow - jumps
#pragma - frerun - cse - after - loop
#pragma - fdelete - null - pointer - checks
#pragma - fextensive - optimizations
#pragma - fregmove
#pragma - fschedule - insns
#pragma - fsched - interblock
#pragma - fcaller - saves
#pragma - fpeephole2
#pragma - freorder - blocks
#pragma - fstrict - aliasing
#pragma - funit - at - a - time
#pragma - falign - functions
#pragma - fcrossjumping
#pragma - finline - functions
#pragma - fweb
using namespace std;
inline ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
ll n;
ll a[maxn];
ll b[maxn];
ll ans;
int main() {
  n = read();
  for (rg int i = 1; i <= n + 1; ++i)
    a[i] = read();
  for (rg int i = 1; i <= n; ++i)
    b[i] = read();
  for (rg int i = n + 1; i >= 1; --i) {
    if (b[i] >= a[i]) {
      ans += a[i];
      b[i] -= a[i];
      a[i] = 0;
    } else {
      a[i] -= b[i];
      ans += b[i];
      b[i] = 0;
      if (b[i - 1] >= a[i]) {
        ans += a[i];

        b[i - 1] -= a[i];
        a[i] = 0;
      } else {

        a[i] -= b[i - 1];
        ans += b[i - 1];
        b[i - 1] = 0;
      }
    }
  }
  cout << ans;
}