PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000024. Trò chơi năng lượng (XYZZY)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trò chơi năng lượng (XYZZY)

XYZZY

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

A game has nn rooms numbered 11 through nn. You begin in room 11 with 100100 energy and aim to enter room nn. Entering room vv adds its energy value eve_v; one-way doorways connect rooms. Revisited rooms contribute their energy every time. Energy must remain strictly positive at each step, otherwise the player dies. Start and finish rooms have energy zero. Determine whether the game is winnable.

Input

The first line contains nn. For each room ii, one record gives its energy eie_i, outgoing-door count kik_i, and kik_i destination room indices.

Output

Print winnable if the final room can be entered with strictly positive energy throughout the walk, or hopeless otherwise.

Subtasks

  • Subtask 1 (20 points): 2≤n≤122\le n\le12; −100≤ei≤100-100\le e_i\le100; 0≤ki≤50\le k_i\le5; e1=en=0e_1=e_n=0; doorways are directed, destination in [1,n][1,n].

  • Subtask 2 (30 points): 2≤n≤502\le n\le50; −500≤ei≤500-500\le e_i\le500; 0≤ki≤150\le k_i\le15; e1=en=0e_1=e_n=0; doorways are directed, destination in [1,n][1,n].

  • Subtask 3 (50 points): 2≤n≤1002\le n\le100; −1000≤ei≤1000-1000\le e_i\le1000; 0≤ki≤1000\le k_i\le100; e1=en=0e_1=e_n=0; doorways are directed, destination in [1,n][1,n].

Examples

Input

5
0 1 2
-60 1 3
-60 1 4
20 1 5
0 0

Output

hopeless

Explanation

Entering room 22 leaves 4040 energy; entering room 33 would reduce it to −20-20, immediately losing.