This page is still under construction.

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

Dividing the Gold

Time limit1sMemory limit128 MB

Summary
Given N coins with values, find the minimum difference between two piles and count the subsets forming the lighter pile, modulo 1,000,000.
Level

Medium6 of 10

Topics
Dynamic programming, Combinatorics, Math, Implementation
Solved
No attempts yet

Problem

Bessie and Canmuu found a sack of NN gold coins that they want to divide as evenly as possible. Coin ii has value viv_i. Splitting the coins into two piles of exactly equal value is not always possible, so they want to make the difference between the two piles as small as they can. What is that smallest difference?

There may also be several ways to achieve that smallest difference. Bessie and Canmuu also want to know the number of ways to split the coins as fairly as possible. If the two piles cannot be made exactly equal, Bessie takes the more valuable pile.

For example, suppose there are five coins of values 2,1,8,4,162, 1, 8, 4, 16. Put the coin of value 1616 in one pile and the rest in the other pile; the other pile is worth 1+2+4+8=151 + 2 + 4 + 8 = 15, so the difference is 16−15=116 - 15 = 1. This is the only way to reach that difference, so the number of fairest splits is 11.

Coins of equal value can be swapped between the piles and still give an optimal split, which can increase the number of ways. For instance, four coins that are all worth 11, i.e. {1,1,1,1}\{1, 1, 1, 1\}, can be split into two piles of two coins each in 66 different ways.

To count the ways, count the number of coin subsets that form the lighter pile — the subset whose total does not exceed half of the overall value and is as close to that half as possible. Coins of equal value are still treated as distinct coins.

Constraints: 1≤N≤2501 \le N \le 250 and 1≤vi≤20001 \le v_i \le 2000.

Input

  • Line 1: a single integer NN.
  • Lines 2 through N+1N+1: line i+1i+1 contains a single integer viv_i, the value of coin ii.

Output

  • Line 1: a single integer, the smallest possible difference between the two piles.
  • Line 2: a single integer, the number of ways to split the coins achieving that minimum difference. Because this number can be very large, print it modulo 1,000,0001{,}000{,}000.

Examples2

  1. Example 1

    Input
    5
    2
    1
    8
    4
    16
    
    Expected output
    1
    1
    
  2. Example 2

    Input
    4
    1
    1
    1
    1
    
    Expected output
    0
    6