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

#GD0000233. Tiêu diệt quái vật theo ngày (Yet Another Monster Killing Problem)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tiêu diệt quái vật theo ngày (Yet Another Monster Killing Problem)

Yet Another Monster Killing Problem

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A dungeon contains nn monsters in order, with strengths aia_i. You have mm heroes; hero jj has power pjp_j and endurance sjs_j.

Each day, select one hero to fight consecutive unbeaten monsters from the front. The hero defeats a monster only if its strength is at most the hero's power, and may defeat at most sjs_j monsters per day. Any hero may be reused on another day, and defeated monsters do not return.

Find the minimum number of days to clear all monsters, or print -1 if impossible.

Input

  • The first line contains nn.
  • The next line contains nn monster strengths aia_i.
  • The following line contains mm.
  • The last mm lines contain each hero's pj,sjp_j,s_j.

Output

Print the minimum number of days, or -1 if completing the dungeon is impossible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 1≤m≤1001\le m\le 100; 1≤ai,pj≤1091\le a_i,p_j\le 10^9; 1≤sj≤n1\le s_j\le n; n+m≤2⋅105n+m\le 2\cdot 10^5.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤m≤100001\le m\le 10000; 1≤ai,pj≤1091\le a_i,p_j\le 10^9; 1≤sj≤n1\le s_j\le n; n+m≤2⋅105n+m\le 2\cdot 10^5.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤m≤2⋅1051\le m\le 2\cdot 10^5; 1≤ai,pj≤1091\le a_i,p_j\le 10^9; 1≤sj≤n1\le s_j\le n; n+m≤2⋅105n+m\le 2\cdot 10^5.

Examples

Input

6
2 3 11 14 1 8
2
3 2
100 1

Output

5

Explanation

On day 11, hero (3,2)(3,2) defeats strengths 2,32,3. On days 2,32,3, hero (100,1)(100,1) defeats strengths 11,1411,14 separately. On day 44, hero (3,2)(3,2) defeats strength 11, and on day 55, hero (100,1)(100,1) defeats strength 88. Total: 55 days.