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: Wrong Answer)

using System;
using System.IO;

namespace contest
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.SetIn(new StreamReader("input.txt"));
            solve_abc135c();
        }

        public static void solve_abc135c()
        {
            int n = Convert.ToInt32(Console.ReadLine());

            string[] testA = Console.ReadLine().Split(' ');
            string[] testB = Console.ReadLine().Split(' ');

            int total = 0;
            int r = 0;
            for (int i = 0; i <= n; i++)
            {
                int a = Convert.ToInt32(testA[i]);
                int b = i == n ? r : Convert.ToInt32(testB[i]) + r;

                total += Math.Min(a, b);
                r = Math.Abs(a - b);
                /*if (a <= b)
                {
                    total += a;
                    r = Math.Abs(a - b);
                }
                else if (a > b)
                {
                    total += b;
                    r = 0;
                }
                else if ( i == n)
                {
                    total += Math.Min(a, b);
                }
*/
            }

            Console.WriteLine(total);
        }
        public static void solve_abc135b()
        {
            int n = Convert.ToInt32(Console.ReadLine());
            string[] test = Console.ReadLine().Split(' ');

            int[] unsorted = Array.ConvertAll(test, int.Parse);
            int[] sorted = Array.ConvertAll(test, int.Parse);;

            Array.Sort(sorted);
            int count = 0;
            for (int i = 0; i < n; i++)
            {
                if (unsorted[i] != sorted[i])
                {
                    count++;
                }
            }
            


            if (count > 2)
            {
                Console.WriteLine("NO");
            }
            else
            {
                Console.WriteLine("YES");
            }
            //TODO: check if it is possible to find within one loop
            /*
            int count = 0;
            for (int i = 0; i < n - 1; i++)
            {
                int cur = Convert.ToInt32(test[i]);
                int next = Convert.ToInt32(test[i + 1]);
                if (cur > next)
                {
                    count++;
                }
            }

            if (count > 2)
            {
                Console.WriteLine("NO");
            }
            else
            {
                Console.WriteLine("YES");
            }
            */
        }
        public static void solve_abc135a()
        {
            string[] ab = Console.ReadLine().Split(' ');

            int a = Convert.ToInt32(ab[0]);
            int b = Convert.ToInt32(ab[1]);

            int res = Math.Abs(a - b);

            if (res % 2 == 0)
            {
                Console.WriteLine(Math.Max(a, b) - res / 2);
            }
            else
            {
                Console.WriteLine("IMPOSSIBLE");
            }

        }

        public static void solve_agc35a()
        {
            int n = Convert.ToInt32(Console.ReadLine());

            string[] test = Console.ReadLine().Split(' ');

            int[] hats = Array.ConvertAll(test, int.Parse);

            Array.Sort(hats);
            string answer = "Yes";
            for (int i = 0; i < hats.Length; i++)
            {
                int prev = 0;
                int next = 0;
                if (i == 0)
                {
                    prev = hats[n - 1];
                    next = hats[i + 1];
                }
                else if (i == n - 1)
                {
                    prev = hats[i - 1];
                    next = hats[0];
                }
                else
                {
                    prev = hats[i - 1];
                    next = hats[i + 1];
                }
                if (hats[i] != (prev ^ next))
                {
                    answer = "No";
                    break;
                }
            }

            Console.WriteLine(answer);
        }

        //Скорее всего можно решить за константу.
        public static void solve_abc125_a()
        {
            string[] abt = Console.ReadLine().Split(' ');
            int a = Convert.ToInt32(abt[0]);
            int b = Convert.ToInt32(abt[1]);
            int c = Convert.ToInt32(abt[2]);

            int count = 0;
            int i = 1;
            while (i * a <= c)
            {
                count += b;
                i++;
            }

            Console.WriteLine(count);
        }

        /*
        Перый подход найти два числа такие, чтобы разность
        суммы ценностей и суммы цен была максимальной неверен. 
        Невнимательно прочитал задание. Там сказано, что можно взять
        несколько или не брать вообще. Может быть не ровно два.
        Решение:
        нужно мксимизировтаь разность ценности и цены элементов.  
        */
        public static void solve_abc125_b()
        {
            int n = Convert.ToInt32(Console.ReadLine());
            string[] values = Console.ReadLine().Split(' ');
            string[] costs  = Console.ReadLine().Split(' ');

            int max = 0;
            for (int i = 0; i < values.Length; i++)
            {
                max = Math.Max(max, max + Convert.ToInt32(values[i]) - Convert.ToInt32(costs[i]));
            }

            Console.WriteLine(max);
        }

        /*
        Первая идея найти два маскимума или маскимум и мимнимум
        и среди них искать gcd не работает. Среди остальных чисел может быть
        несколько простых чисел, которые не подойдут.
         */
        public static void solve_abc125_c()
        {
            int n = Convert.ToInt32(Console.ReadLine());
            string[] str = Console.ReadLine().Split(' ');
            int[] nums = Array.ConvertAll(str, int.Parse);

            int max = nums[0];
            for (int i = 1; i < nums.Length; i++)
            {
                int temp = gcd(max, nums[i]);
                if (max < temp)
                {
                    max = temp;
                }
                /*else if (chageElement)
                {
                    max = 
                }*/
            }

            Console.WriteLine(max);
        }

        public static int gcd(int a, int b)
        {
            if (b == 0)
            {
                return a;
            }
            return gcd(b, a % b);
        }

        public static void generate()
        {
    //         	public static void main(String[] args) {
	// 	int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	// 	int mask = 0;
	// 	int limit = 1 << array.length;
	// 	while (mask < limit) {
	// 		printPermutation(mask, array);
	// 		mask++;
	// 	}
	// }
 
	// private static void printPermutation(int mask, int[] array) {
	// 	System.out.print("(");
	// 	for (int i = 0; i < array.length; i++) {
	// 		//исли бит номер i еденичный, печатаем i-й элемент массива
	// 		if ((mask & (1 << i)) != 0) {
	// 			System.out.print(array[i] + ",");
	// 		}
	// 	}
	// 	System.out.println(")");
	// }
        }
    }
}