This page is still under construction.

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

Maximum Subsequence

Time limit2sMemory limit1024 MB

Summary
Permute a sequence of up to 16 integers so that its maximum contiguous subarray sum is as small as possible, and print that minimum.
Level

Medium6 of 10

Topics
Dynamic programming, Bit manipulation, Greedy, Sorting
Solved
No attempts yet

Problem

For a sequence a1...na_{1...n}, define f(a)f(a) as

f(a)=max⁡1≤l≤r≤n∑i=lrai.f(a) = \max\limits_{1 \le l \le r \le n} \sum\limits_{i = l}^{r} a_i\text{.}

Given a sequence b1...nb_{1...n}, you need to permute b1...nb_{1...n} to obtain b1...n′b'_{1...n} and minimize f(b′)f(b').

Input

The first line contains a single integer nn (1≤n≤161 \le n \le 16).

The second line contains nn integers a1...na_{1...n} (∣ai∣≤105|a_i| \le 10^5).

Output

Output the minimum possible value of f(b′)f(b').

Examples2

  1. Example 1

    Input
    4
    1 -1 1 1
    
    Expected output
    2
    
  2. Example 2

    Input
    6
    4 -4 5 -20 6 7
    
    Expected output
    9