PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000038. Ăn kẹo (Eating Candies)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ăn kẹo (Eating Candies)

Eating Candies

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn candies in a row; candy ii weighs wiw_i. Alice may eat a prefix from the left, and Bob may eat a suffix from the right. Their chosen candies must be disjoint. They want the total weights they eat to be equal; either player may eat zero candies. Determine the maximum combined number of eaten candies.

Input

  • The first line contains nn, the number of candies.
  • The second line contains nn integers w1,…,wnw_1,\ldots,w_n in left-to-right order.

Output

Print the maximum total number of eaten candies while both players eat equal total weights.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le8; 1≤wi≤1041\le w_i\le10^4 for all 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤wi≤1041\le w_i\le10^4 for all 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le200000; 1≤wi≤1041\le w_i\le10^4 for all 1≤i≤n1\le i\le n.

Examples

Input

6
2 1 4 2 4 1

Output

6

Explanation

Alice eats the left three candies, weights 2,1,42,1,4, total 77. Bob eats the right three, weights 2,4,12,4,1, also total 77. The chosen sets are disjoint and cover all six candies, so the answer is 66.