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

#BS0000337. Mua vật phẩm giảm giá (bản khó) (Microtransactions (hard version))

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Mua vật phẩm giảm giá (bản khó) (Microtransactions (hard version))

Microtransactions (hard version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn item types. An must purchase exactly kik_i units of type ii. Starting on day 1, An receives one coin each morning and saves unused coins. Each evening any number of items can be bought within the available money. The regular unit price is two coins. Offer (dj,tj)(d_j,t_j) makes type tjt_j cost one coin per unit on day djd_j. Offers may coincide. Find the earliest day on which all required items can be purchased.

Input

First line: n,mn,m. Second line: nn integers kik_i. Each of the following mm lines contains dj,tjd_j,t_j.

Output

Print the minimum day.

Subtasks

  • Subtask 1 (20 points): 1≤n≤31\le n\le3; 1≤m≤81\le m\le8; 0≤ki0\le k_i; 1≤∑ki≤51\le\sum k_i\le5; 1≤dj≤81\le d_j\le8; 1≤tj≤n1\le t_j\le n.
  • Subtask 2 (30 points): 1≤n,m≤2⋅1051\le n,m\le2\cdot10^5; 0≤ki≤2⋅1050\le k_i\le2\cdot10^5; 1≤∑ki≤2⋅1051\le\sum k_i\le2\cdot10^5; 1≤dj≤2⋅1051\le d_j\le2\cdot10^5; 1≤tj≤n1\le t_j\le n; all offers have the same day.
  • Subtask 3 (50 points): 1≤n,m≤2⋅1051\le n,m\le2\cdot10^5; 0≤ki≤2⋅1050\le k_i\le2\cdot10^5; 1≤∑ki≤2⋅1051\le\sum k_i\le2\cdot10^5; 1≤dj≤2⋅1051\le d_j\le2\cdot10^5; 1≤tj≤n1\le t_j\le n.

Examples

Input 1

2 2
2 1
1 1
3 1

Output 1

4

Explanation

On day 3 buy two type-1 items for two coins. On day 4 buy the type-2 item for two coins. At least four coins are required.

Input 2

1 1
3
10 1

Output 2

6

Explanation

The offer comes too late; buying all three at regular price takes six days.