This page is still under construction.

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

Escaping the Farm

Time limit1sMemory limit128 MB

Summary
Among up to 20 cow weights, find the largest subset whose base-10 sum produces no carry in any digit position.
Level

Medium5 of 10

Topics
Bit manipulation, Brute force, Backtracking
Solved
No attempts yet

Problem

The cows have hatched a daring plan to escape from Farmer John's clutches. They have gotten hold of a small inflatable raft, and under cover of night a group of cows will board the raft and row across the river that borders the farm. The plan seems perfect — until the cows realize that their tiny raft may not be able to hold much weight!

The NN cows (1≤N≤201 \le N \le 20) have weights w1,…,wNw_1, \dots, w_N. To decide whether a group of cows is light enough not to sink the raft, the cows add up all of the weights in the group. Unfortunately the cows are notoriously bad at arithmetic: if adding the weights of the cows in a group produces any carry (using standard base-10 addition), the cows give up and conclude that the group must be too heavy for the raft. Any group whose weights can be added with no carries is assumed to be light enough to fit on the raft.

Help the cows determine the size of the largest group they believe can fit on the raft — that is, the largest group whose weights can be added together with no carries.

Input

  • Line 1: the number of cows, NN (1≤N≤201 \le N \le 20).
  • Lines 2 to N+1N+1: each line contains the weight of one cow, an integer between 1 and 100,000,000 inclusive.

Output

  • Line 1: the number of cows in the largest group whose weights can be added together with no carries.

Hint

Input Details

There are 5 cows, with weights 522, 6, 84, 7311, and 19.

Output Details

The three weights 522, 6, and 7311 can be added together with no carries:

   522
     6
+ 7311
------
  7839

Examples1

  1. Example 1

    Input
    5
    522
    6
    84
    7311
    19
    
    Expected output
    3