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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using static Input;

public class Prog
{
    public struct Pair { public int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } }
    public static void Solve()
    {
        int N = NextInt();
        long[] A = LineLong();
        long[] B = LineLong();
        long ans = A.Sum();
        for (int i = 0; i < N; i++)
        {
            long F_m = Math.Min(A[i], B[i]);
            A[i] -= F_m;
            B[i] -= F_m;
            if (B[i] > 0)
            {
                F_m = Math.Min(A[i + 1], B[i]);
                A[i + 1] -= F_m;
                B[i] -= F_m;
            }
        }
        Console.WriteLine(ans - A.Sum());
    }
    public static void Main()
    {
        var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }
    private static int UpperBound(int[] list, int n)
    {
        int a = 0, b = list.Count();
        if (list[a] >= n) return a;
        else if (list[b - 1] < n) return b;
        while (b - a != 1)
        {
            int x = (a + b) / 2;
            if (list[x] >= n) b = x;
            else a = x;
        }
        return b;
    }
    private static int LowerBound(int[] list, int n)
    {
        int a = 0, b = list.Count();
        if (list[b - 1] <= n) return b - 1;
        else if (list[a] > n) return a - 1;
        while (b - a != 1)
        {
            int x = (a + b) / 2;
            if (list[x] <= n) a = x;
            else b = x;
        }
        return a;
    }
}
public class PriorityQueue<Tkey, Tvalue> where Tkey : IComparable
{
    private List<Tkey> keys = new List<Tkey>();
    private List<Tvalue> values = new List<Tvalue>();
    private int size = 0;
    private static bool Descending;
    private static int Compare(Tkey a, Tkey b) { return (Descending ? a.CompareTo(b) : b.CompareTo(a)); }

    public PriorityQueue(bool b = true) { Clear(); Descending = b; }
    public int Count() { return size; }
    public void Clear() { keys = new List<Tkey>(); values = new List<Tvalue>(); size = 0; }
    public void Add(Tkey e, Tvalue v)
    {
        keys.Add(e);
        values.Add(v);
        int x = size;
        size += 1;
        while (x != 0)
        {
            int par = (x % 2 == 0 ? x - 2 : x - 1) / 2;
            if (Compare(keys[par], e) <= 0) break;
            Tkey a = keys[x]; keys[x] = keys[par]; keys[par] = a;
            Tvalue b = values[x]; values[x] = values[par]; values[par] = b;
            x = par;
        }
    }
    public void Poll()
    {
        Tkey k = keys[size - 1];
        Tvalue v = values[size - 1];
        int x = 0;
        size--;
        if (size == 0) { Clear(); return; }
        keys.RemoveAt(size);
        values.RemoveAt(size);
        while (true)
        {
            int l = x * 2 + 1;
            int r = x * 2 + 2;
            if (size - 1 < l) break;
            int y = 0;
            if (size - 1 == l) { y = l; }
            else if (Compare(keys[l], keys[r]) == 1) { y = r; }
            else { y = l; }
            if (Compare(keys[y], k) > 0) break;
            keys[x] = keys[y];
            values[x] = values[y];
            x = y;
        }
        keys[x] = k;
        values[x] = v;
    }
    public Tvalue Take() { Tvalue ret = values[0]; Poll(); return ret; }
    public Tvalue Peek() { return values[0]; }
    public void Debug()
    {
        Console.Write("key   : ");
        foreach (var k in keys) Console.Write(k + " ");
        Console.WriteLine();
        Console.Write("value : ");
        foreach (var k in values) Console.Write(k + " ");
        Console.WriteLine();
    }
}
public class Node
{
    public List<Edge> relate;
    public bool visit = false;
    public long cost = long.MaxValue;
    public long plus_cost = 0;
    public Node() { relate = new List<Edge>(); }
}
public class Edge
{
    public int to;
    public long cost;
    public long T;
    public Edge(int to, long cost, long T) { this.to = to; this.cost = cost; this.T = T; }
}
public class Input
{
    public const long MOD = 1000000007;
    public const int INF = 1000000007;
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
    public long Abs(long a, long b) => Math.Abs(a - b);
}