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 Rust (1.15.1) (judge verdict: Wrong Answer)

fn main() {
    let mode = "c";
    if mode == "a" {
        a::solve();
    } else if mode == "b" {
        b::solve();
    } else if mode == "c" {
        c::solve();
    }
}

mod c {
    use std;
    use std::io::BufRead;

    pub fn solve() {
        let stdin = std::io::stdin();
        let mut lines = stdin.lock().lines();

        lines.next();

        let a = lines
            .next()
            .map(|x| {
                x.unwrap()
                    .split_whitespace()
                    .map(|x| x.parse::<i32>().unwrap())
                    .collect()
            })
            .unwrap();
        //        println!("a: {:?}", a);

        let b = lines
            .next()
            .map(|x| {
                x.unwrap()
                    .split_whitespace()
                    .map(|x| x.parse::<i32>().unwrap())
                    .collect()
            })
            .unwrap();
        //        println!("b: {:?}", b);

        let res = calc(a, b);
        println!("{}", res);
    }

    fn calc(mut a: Vec<i32>, mut b: Vec<i32>) -> i32 {
        let mut total_killed = 0;
        for i in 0..b.len() {
            let killed = std::cmp::min(a[i], b[i]);
            total_killed += killed;
            a[i] -= killed;
            b[i] -= killed;

            let killed = std::cmp::min(a[i + 1], b[i]);
            total_killed += killed;
            a[i + 1] -= killed;
            b[i] -= killed;
        }
        total_killed
    }
}

mod b {
    use std;
    use std::io::Read;

    pub fn solve() {
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf).unwrap();

        let mut text = buf.split_whitespace();
        text.next();

        let input: Vec<i32> = text.map(|x| x.parse().unwrap()).collect();
        let res = if calc(input) { "YES" } else { "NO" };
        println!("{}", res);
    }
    fn calc(mut p: Vec<i32>) -> bool {
        let ng_point = match search_ng(&p) {
            Some(x) => x,
            None => return true,
        };
        let min_point = match search_min(&p, ng_point + 1) {
            Some(x) => x,
            None => return true,
        };
        p.swap(ng_point, min_point);
        is_sorted(p)
    }
    fn is_sorted(p: Vec<i32>) -> bool {
        for i in 0..(p.len() - 1) {
            if p[i] > p[i + 1] {
                return false;
            }
        }
        true
    }

    fn search_ng(p: &Vec<i32>) -> Option<usize> {
        for i in 0..(p.len() - 1) {
            if p[i] > p[i + 1] {
                return Some(i);
            }
        }
        None
    }

    fn search_min(p: &Vec<i32>, start: usize) -> Option<usize> {
        let mut min = std::i32::MAX;
        let mut point = None;
        for i in start..p.len() {
            if p[i] < min {
                min = p[i];
                point = Some(i);
            }
        }
        point
    }
}

mod a {
    use std;
    use std::io::Read;

    pub fn solve() {
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf).unwrap();

        let mut text = buf.split_whitespace();
        let a = text.next().unwrap().parse().unwrap();
        let b = text.next().unwrap().parse().unwrap();
        match calc(a, b) {
            None => println!("IMPOSSIBLE"),
            Some(x) => println!("{}", x),
        }
    }

    fn calc(a: i32, b: i32) -> Option<i32> {
        let total = a + b;
        if total & 0x1 == 0x1 {
            return None;
        }
        Some(total / 2)
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn test1() {
            assert_eq!(calc(2, 16), Some(9));
        }
    }
}