Problem:
Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<string>
#include<cstdio>
#include <cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = 1e+14;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back

void Yes(){
	cout<<"Yes"<<endl;
	exit(0);
}
 
void No(){
	cout<<"No"<<endl;
	exit(0);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
    int N; cin >> N;
    ll A[100100], A_[100100], B[100100], B_[100100];
    rep(i, N + 1) {
        cin >> A[i];
        A_[i] = A[i];
    }
    rep(i, N) {
        cin >> B[i];
        B_[i] = B[i];
    }
    ll cnt1 = 0, cnt2 = 0;
    rep(i, N) {
        if(B[i] <= A[i]) {
            cnt1 += B[i];
            continue;
        } else {
            cnt1 += A[i];
            B[i] -= A[i];
            if(B[i] <= A[i + 1]) {
                cnt1 += B[i];
                A[i + 1] -= B[i];
            } else {
                cnt1 += A[i + 1];
                A[i + 1] = 0;
            }
        }
    }
    per(i, N) {
        if(B_[i] <= A_[i + 1]) {
            cnt2 += B_[i];
            continue;
        } else {
            cnt2 += A_[i + 1];
            B_[i] -= A_[i + 1];
            if(B_[i] <= A_[i]) {
                cnt2 += B_[i];
                A_[i] -= B_[i];
            } else {
                cnt2 += A_[i];
                A_[i] = 0;
            }
        }
    }
    ll ans = max(cnt1, cnt2);
    cout << ans << endl;
	return 0;
}