아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Cooking

시간 제한1초메모리 제한512 MB

요약
각 요리 i가 정확히 a_i번 등장하도록 요리 두 개(같아도 됨)를 짝지어 총 조리 시간을 최소화하고, 불가능하면 -1을 출력한다.
난이도

보통10점 중 7점

유형
그래프, 동적 계획법, 조합론
정답자
아직 제출이 없습니다

문제

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 nn different dishes. After buying all the necessary food, the guys have decided that before they go shopping next time, they will cook the ii-th dish exactly a_ia\_i times. 

Each day Sergey and Stephen choose two dishes ii and jj and cook them, cooking takes c_i,jc\_{i, j} units of time. It's possible that i=ji=j, that means that the ii-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!

입력

The first line contains a single integer nn (1≤n≤101 \le n \le 10) --- the number of dishes that the guys can cook. 

The second line contains nn positive integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (1≤a_i≤501 \leq a\_i \leq 50) --- the number of times it must be cooked for each dish.

The next nn lines contain nn space separated integers c_i,jc\_{i, j} (1≤c_i,j≤1001 \le c\_{i, j} \le 100), the jj-th number in the ii-th line denotes cooking time of pair of dishes ii and jj. It is guaranteed that c_i,j=c_j,ic\_{i, j}=c\_{j, i}.

출력

Print one integer: the minimum total time of cooking, or −1-1, if it is impossible to make a cooking plan such that the ii-th dish is cooked exactly a_ia\_i times.

힌트

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

예제3

  1. 예제 1

    입력
    3
    2 2 2
    1 4 3
    4 4 5
    3 5 6
    
    예상 출력
    10
    
  2. 예제 2

    입력
    2
    2 39
    23 9
    9 23
    
    예상 출력
    -1
    
  3. 예제 3

    입력
    1
    2
    100
    
    예상 출력
    100