Hydra (Problem concept)

Revision en1, by Im9223372036854775808, 2025-12-03 11:45:17

Description

There is a Hydra, that you need to slay: it has n types of heads, numbered from 1 to n («lower» till «higher»). You can strike one head and cut it, but every head produces other heads. Your goal is to slay the hydra with minimum amount of strikes. Hydra is killed, when after cutting the last head, other won't appear.

Input

First string contains n — nuuber of head types (1 ≤ n ≤ 2000). The second string contains n elements: a_1 ... a_n (0 ≤ a_i ≤ 10^9) — starting quantity of every heads. Then comes n string, containing n nums (matrix b).

Output

Minimum amount of strikes to slay the Hydra. Write -1 if it is impossible

Примеры

Пример 1

Вход:

  • 3
  • 1 0 0
  • 0 1 2
  • 0 0 0
  • 0 0 0

Выход:

  • 4

Пример 2

Вход:

  • 5
  • 1 1 1 1 1
  • 0 1 0 0 0
  • 0 0 1 0 0
  • 0 0 0 1 0
  • 0 0 0 0 1
  • 0 0 0 0 0

Выход:

  • 31

Пример 3:

Вход:

  • 1
  • 1
  • 1

Выход:

  • -1

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
ru6 Russian Im9223372036854775808 2025-12-04 11:37:05 38
en2 English Im9223372036854775808 2025-12-03 12:01:37 119
en1 English Im9223372036854775808 2025-12-03 11:45:17 1020 Initial revision for English translation
ru5 Russian Im9223372036854775808 2025-12-03 11:05:48 2 Мелкая правка: '- 1\n- 1\n#### Вых' -> '- 1\n- 1\n\n#### Вых'
ru4 Russian Im9223372036854775808 2025-12-03 11:05:29 12
ru3 Russian Im9223372036854775808 2025-12-03 11:04:58 44
ru2 Russian Im9223372036854775808 2025-12-02 22:51:10 10
ru1 Russian Im9223372036854775808 2025-12-02 18:57:19 1279 Первая редакция (опубликовано)