Under construction

Still being built. Things may change

Buying Potions

Interview

Time limit3sMemory limit1024 MB

Summary
Given N potion prices and the discounts each purchase triggers on other potions, find the minimum total cost to buy all of them in some order.
Level

Medium4 of 10

Topics
Bit manipulation, Dynamic programming
Solved
No attempts yet

Problem

Jun-gyeom is an adventurer. Before setting out on an adventure, he has to prepare thoroughly.

Before leaving, Jun-gyeom wants to buy all NN kinds of potions. At the potion shop, he finds that each potion is numbered from 1 to NN. The shop is running a special event today: buying a certain potion gives discounts on some other potions.

The original price of the ii-th potion is cic_i coins. When you buy the ii-th potion, the prices of pip_i other potions go down.

Discounts stack. For example, suppose buying potion 1 takes 1 coin off potion 3, and buying potion 2 takes 2 coins off potion 3. If you buy both, then buying potion 3 afterward gives a discount of 3 coins. A price never drops below 0. For example, if a potion originally costs 5 coins and is discounted by more than 4 coins, its price becomes 1 coin.

Jun-gyeom was excited to start buying, but then he realized that the order of purchases matters. Find the minimum total cost of buying all the potions.

Input

The first line contains NN, the number of potion kinds.

The second line contains the prices cic_i separated by spaces (1≤i≤N1 \le i \le N).

The next lines give the discount information for each potion, for ii from 1 to NN (1≤i≤N1 \le i \le N).

Each block starts with pip_i. The next pip_i lines each contain a potion number aja_j and a discount amount djd_j. This means that after you buy potion ii, potion aja_j is discounted by djd_j coins.

Output

Print the minimum number of coins needed to buy all the potions, on the first line.

Constraints

  • 2≤N≤102 \le N \le 10
  • 1≤ci≤1 0001 \le c_i \le 1\,000
  • 0≤pi≤N−10 \le p_i \le N-1
  • For each ii, all aja_j are distinct and aj≠ia_j \neq i
  • 1≤dj≤1 0001 \le d_j \le 1\,000

Examples1

  1. Example 1

    Input
    4
    10 15 20 25
    2
    3 10
    2 20
    0
    1
    4 10
    1
    1 10
    
    Expected output
    36