Under construction

Still being built. Things may change

Baking Cakes

Time limit1sMemory limit128 MB

Summary
Given up to 40 cake baking times and 3 ovens, find the minimum time to bake all cakes.
Level

Medium4 of 10

Topics
Dynamic programming, Greedy, Brute force, Math
Solved
No attempts yet

Problem

Tom's birthday is coming up, and you have been put in charge of baking cakes for his enormous birthday party. However, you have a great many cakes to make and very little time, so you are not sure you will even finish before the party!

You are given a list of cakes to make, each requiring a certain amount of time to bake. You have exactly 3 ovens, and each oven can bake only one cake at a time. Assuming that the time required to take one cake out and put another one in is negligible, determine the smallest amount of time you will need to spend baking, given the list of cakes to make.

Input

The input consists of multiple test cases, each on a single line. Each line begins with an integer nn (where 1≤n≤401 \le n \le 40), the number of cakes to bake. It is followed by nn integers t1,…,tnt_1, \dots, t_n (where 1≤ti≤301 \le t_i \le 30), the time in minutes required to bake each cake. The end of input is marked by a line containing a single 00; do not process this line.

Output

For each test case, output on a single line the smallest amount of time, in minutes, that you need to bake all of your cakes.

Examples1

  1. Example 1

    Input
    1 30
    3 15 10 20
    5 6 7 8 9 10
    0
    
    Expected output
    30
    20
    15