Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});

Farm Village

There is a village along a road. This village has $N$ houses numbered $1$ to $N$ in order along the road. Each house has a field that can make up to two units of the crop and needs just one unit of the crop. The total cost to distribute one unit of the crop to each house is the summation of carrying costs and growing costs.

The carrying cost: The cost to carry one unit of the crop between the $i$-th house and the ($i+1$)-th house is $d_i$. It takes the same cost in either direction to carry.

The growing cost: The cost to grow one unit of the crop in the $i$-th house's field is $g_i$.

Your task is to calculate the minimum total cost to supply one unit of the crop to each house.

Input

The input consists of a single test case formatted as follows.

$N$
$d_1$ ... $d_{N-1}$
$g_1$ ... $g_{N}$

The first line consists of an integer $N$ ($2 \leq 200,000$), which is the number of the houses. The second line consists of $N-1$ integers separated by spaces. The $i$-th integer $d_i$ ($1 \leq d_i \leq 10^9$, $1 \leq i \leq N-1$)represents the carrying cost between the $i$-th and the ($i+1$)-th houses. The third line consists of $N$ integers separated by spaces. The $i$-th integer $g_i$ ($1 \leq g_i \leq 10^9$, $1 \leq i \leq N$) represents the growing cost of the $i$-th house's field.

Output

Print the minimum cost to supply one unit of the crop to each house.

Sample Input 1

2
3
1 5

Output for Sample Input 1

5

Sample Input 2

3
100 100
1 2 3

Output for Sample Input 2

6

Sample Input 3

4
1 2 3
1 1 100 100

Output for Sample Input 3

12

Solution in JAVA (judge verdict: Wrong Answer)


import java.io.*;
import java.util.*;

/**
 * AIZU ONLINE JUDGE
 * 2865 Farm Village
 *    2018/02/23
 */
public class Main {

    
    int calc(int N, int[] d, int[] g) {
        
        int[] crop = new int[N];
        
        int sum = 0;
        for(int i = 0; i < N; i++) {
            int bestFrom = 0;
            int bestCost = Integer.MAX_VALUE;
            for(int from = 0; from < N; from++) {
                int cost = 0;
                if (crop[from] >= 2)
                    continue;
                cost += g[from];
                for(int k = Math.min(i, from); k < Math.max(i, from); k++) {
                    cost += d[k];
                }
                if (cost < bestCost) {
                    bestCost = cost;
                    bestFrom = from;
                }
            }
            log.printf("%d from%d cost%d\n", i, bestFrom, bestCost);
            crop[bestFrom]++;
            sum += bestCost;
        }
        return sum;
    }

    boolean main() throws IOException {

        Scanner sc = new Scanner(systemin);

        int N = sc.nextInt();
        int[] d = new int[N - 1];
        int[] g = new int[N];
        for(int i = 0; i < N - 1; i++) {
            d[i] = sc.nextInt();
        }
        for(int i = 0; i < N; i++) {
            g[i] = sc.nextInt();
        }

        int n = calc(N, d, g);
        result.printf("%d\n", n);

        sc.close();
        return true;
    }

    PrintStream log;
    PrintStream result = System.out;
    BufferedReader systemin;

    static Main instance = new Main();

    Main() {
        systemin = new BufferedReader(new InputStreamReader(System.in));
        log = new PrintStream(new OutputStream() { public void write(int b) {} } );
    }

    public static void main(String[] args) throws IOException {

        instance.main();

        instance.systemin.close();
    }


}