407787: GYM102893 G Cooking

Memory Limit:512 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

G. Cookingtime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard output

Stephen and Sergey are enrolled at the university and live on campus. So now they need to learn to cook.

The friends have learned to cook $$$n$$$ different dishes. After buying all the necessary food, the guys have decided that before they go shopping next time, they will cook the $$$i$$$-th dish exactly $$$a_i$$$ times.

Each day Sergey and Stephen choose two dishes $$$i$$$ and $$$j$$$ and cook them, cooking takes $$$c_{i, j}$$$ units of time. It's possible that $$$i=j$$$, that means that the $$$i$$$-th dish is cooked twice on that day.

The guys are quite lazy, so they want to minimize the total cooking time for all the days before the next shopping. Help them to do it!

Input

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10$$$) — the number of dishes that the guys can cook.

The second line contains $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 50$$$) — the number of times it must be cooked for each dish.

The next $$$n$$$ lines contain $$$n$$$ space separated integers $$$c_{i, j}$$$ ($$$1 \le c_{i, j} \le 100$$$), the $$$j$$$-th number in the $$$i$$$-th line denotes cooking time of pair of dishes $$$i$$$ and $$$j$$$. It is guaranteed that $$$c_{i, j}=c_{j, i}$$$.

Output

Print one integer: the minimum total time of cooking, or $$$-1$$$, if it is impossible to make a cooking plan such that the $$$i$$$-th dish is cooked exactly $$$a_i$$$ times.

ExamplesInput
3
2 2 2
1 4 3
4 4 5
3 5 6
Output
10
Input
2
2 39
23 9
9 23
Output
-1
Input
1
2
100
Output
100
Note

In the first test case it is optimal to cook the following pairs of dishes: $$$(1, 3)$$$, $$$(1, 3)$$$, $$$(2, 2)$$$.

加入题单

算法标签: