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

#TP0000041. Chia mảng thành ba phần (Three Parts of the Array)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chia mảng thành ba phần (Three Parts of the Array)

Three Parts of the Array

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a sequence of nn positive integers d1,…,dnd_1,\ldots,d_n, split it into three consecutive parts in order: prefix, middle part, and suffix. Any part may be empty. The prefix and suffix must have equal sums. Among all valid splits, maximize their common sum. An empty part has sum 00.

Input

  • The first line contains nn, the sequence length.
  • The second line contains exactly nn integers d1,…,dnd_1,\ldots,d_n.

Output

Print the maximum possible common sum of the disjoint prefix and suffix.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le8; 1≤di≤1091\le d_i\le10^9 for all 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤di≤1091\le d_i\le10^9 for all 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le200000; 1≤di≤1091\le d_i\le10^9 for all 1≤i≤n1\le i\le n.

Examples

Input

5
1 3 1 1 4

Output

5

Explanation

Take prefix [1,3,1][1,3,1], whose sum is 55, and suffix [1,4][1,4], also sum 55; the middle is empty. Both parts are disjoint and together use the entire array. They cannot both be extended.