Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rkc                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
typedef long long ll;
#define fl(i, a, b) for (ll i = a; i < b; i++)
#define rfl(i, a, b) for (ll i = a; i > b; i--)
#define pb push_back
#define mp make_pair
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define lld long double
#define vpp vector<pair<ll, ll>>
#define ff first
#define ss second
const ll M = 1e9 + 7;
const double pi = 3.141592653589793238463;
int main() {
  rkc;
  ll n, ans = 0, p = 0, q = 0;
  cin >> n;
  ll a[n + 1], b[n];
  fl(i, 0, n + 1) cin >> a[i];
  fl(i, 0, n) cin >> b[i];
  /*for(ll i=0,j=0;i<n+1,j<n+1;)
  {
      if(a[i]==b[j])
      {
          a[i]=0;
          b[j]=0;
          ans+=a[i];
          i++;j++;
      }
      else if(a[i]>b[j])
      {
          a[i]=a[i]-b[j];
          //b[i]=0;
          ans+=b[j];if(a[i]!=0&&i!=n){i++;j++;}else if(j!=n-1) j++;else break;
          //if(i>j&&j!=n-1)j++;
          //else{
          //i++;j++;}
      }
      else
      {
          ans+=a[i];
          a[i]=0;
          b[j]=b[j]-a[i];
          i++;
      }
  }
  cout<<ans;*/
  /*for(ll i=0,j=0;i<n+1,j<n;)
  {
      if(a[i]>b[j]){if(i>n||j>n-1)break;else {ans+=b[j];i++;j++;}}
      else if(a[i]<b[j])
      {
          if(i>n||j>n-1)break; else {
          ans+=a[i];
          b[j]-=a[i];
          if(abs(i-j)<2)i++;
          else {i++;j++;}
          }
      }
      else
      {
          if(i>n||j>n-1)break;
          else{
          ans+=a[i];i++;j++;
      }}
      //cout<<ans<<endl;
  }
  cout<<ans;*/

  for (ll i = 0, j = 0; i < n + 1, j < n;) {
    if (a[i] > b[j]) {
      ans += b[i];
      i++;
      j++;
    } else if (a[i] < b[j]) {
      ans += a[i];
      b[j] -= a[i];
      p = a[i + 1];
      q = a[i + 1] - b[j];
      /// a[i+1]-=b[j];
      if (q >= 0) {
        a[i + 1] -= b[j];
        ans += b[j];
      } else {
        a[i + 1] = 0;
        ans += p;
      }
      i += 2;
      j++;
    } else {
      ans += a[i];
      i++;
      j++;
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rkc                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
typedef long long ll;
#define fl(i, a, b) for (ll i = a; i < b; i++)
#define rfl(i, a, b) for (ll i = a; i > b; i--)
#define pb push_back
#define mp make_pair
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define lld long double
#define vpp vector<pair<ll, ll>>
#define ff first
#define ss second
const ll M = 1e9 + 7;
const double pi = 3.141592653589793238463;
int main() {
  rkc;
  ll n, ans = 0, p = 0, q = 0;
  cin >> n;
  ll a[n + 1], b[n];
  fl(i, 0, n + 1) cin >> a[i];
  fl(i, 0, n) cin >> b[i];
  /*for(ll i=0,j=0;i<n+1,j<n+1;)
  {
      if(a[i]==b[j])
      {
          a[i]=0;
          b[j]=0;
          ans+=a[i];
          i++;j++;
      }
      else if(a[i]>b[j])
      {
          a[i]=a[i]-b[j];
          //b[i]=0;
          ans+=b[j];if(a[i]!=0&&i!=n){i++;j++;}else if(j!=n-1) j++;else break;
          //if(i>j&&j!=n-1)j++;
          //else{
          //i++;j++;}
      }
      else
      {
          ans+=a[i];
          a[i]=0;
          b[j]=b[j]-a[i];
          i++;
      }
  }
  cout<<ans;*/
  /*for(ll i=0,j=0;i<n+1,j<n;)
  {
      if(a[i]>b[j]){if(i>n||j>n-1)break;else {ans+=b[j];i++;j++;}}
      else if(a[i]<b[j])
      {
          if(i>n||j>n-1)break; else {
          ans+=a[i];
          b[j]-=a[i];
          if(abs(i-j)<2)i++;
          else {i++;j++;}
          }
      }
      else
      {
          if(i>n||j>n-1)break;
          else{
          ans+=a[i];i++;j++;
      }}
      //cout<<ans<<endl;
  }
  cout<<ans;*/

  for (ll i = 0, j = 0; i < n + 1, j < n;) {
    if (a[i] > b[j]) {
      ans += b[i];
      i++;
      j++;
    } else if (a[i] < b[j]) {
      ans += a[i];
      b[j] -= a[i];
      p = a[i + 1];
      q = a[i + 1] - b[j];
      /// a[i+1]-=b[j];
      if (q >= 0) {
        a[i + 1] -= b[j];
        ans += b[j];
      } else {
        a[i + 1] = 0;
        ans += p;
      }
      i++;
      j++;
    } else {
      ans += a[i];
      i++;
      j++;
    }
  }
  cout << ans;
}