This page is still under construction.

Parts of this page are still being built. What you see may change.

Splitting the Snack

Interview

Time limit1sMemory limit128 MB

Summary
Choose which of the N-1 cut points to cut so that both people get exactly N/2 pieces, minimizing the total force of the chosen cuts.
Level

Medium6 of 10

Topics
Dynamic programming, Prefix sum, Array, Greedy
Solved
No attempts yet

Problem

There is a stick-shaped snack of length NN. The snack is made of NN unit-length pieces glued in a row, so there are N−1N-1 cut points, one between each pair of adjacent pieces. The force needed to cut through a point may differ from point to point.

Seonggwan and Dotori want to cut the snack into several pieces and share it so that the total length each person takes is exactly N/2N/2. A point where two pieces going to different people meet must be cut, but there is no need to cut anywhere inside a stretch that a single person takes whole.

Given the force required at each cut point, find the minimum possible total force needed to divide the snack this way.

For example, suppose the snack has length 66 and the forces required to cut each point, from left to right, are {1,8,12,6,2}\{1, 8, 12, 6, 2\}. Then cutting as shown below gives the smallest possible total force, 77.

Input

The first line contains the length of the snack NN. (2≤N≤10,0002 \le N \le 10{,}000, and NN is even.)

From the second line through the NN-th line, the force PP needed to cut each point, from left to right, is given one per line. (0≤P≤10,0000 \le P \le 10{,}000)

Output

Print the minimum total force required, on a single line.

Examples1

  1. Example 1

    Input
    6
    1
    8
    12
    6
    2
    
    Expected output
    7