33OJ
登录 注册

#P17892. [ICPC 2026 Nanchang I] Critical Perfect

    ID: 20628 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>江西ICPC2026省赛/邀请赛

[ICPC 2026 Nanchang I] Critical Perfect

Problem Description

Fengmi is challenging a high-difficulty level in a music game. There are nn notes falling in order, and the ii-th note has a base score aia_i. When the player hits the first ii notes, the cumulative score si=∑k=1iaks_i = \sum_{k=1}^i a_k determines the trigger condition for the combo effect.

Specifically, the ii-th note has a target interval [li,ri][l_i, r_i] for the cumulative score. Only if sis_i falls into this interval can the Critical Perfect combo effect performance be triggered; otherwise, the effect chain will be interrupted.

As the owner of the “Area Amplification” item, Fengmi can use this item any number of times. Each use allows her to select a contiguous segment of notes [l,r][l, r] and increase the base score of each note in this segment by 11. The number of uses affects the final level score, so Fengmi wants to achieve a Critical Perfect combo throughout the entire level with the minimum number of uses.

Please help her calculate the minimum number of times the Area Amplification item must be used so that for every note ii, the cumulative score sis_i satisfies li≤si≤ril_i \le s_i \le r_i, or report that no solution exists.

Input Format

There are multiple test cases. The first line contains an integer tt (1≤t≤50001 \le t \le 5000), the number of test cases. For each test case:

The first line contains an integer nn (1≤n≤50001 \le n \le 5000) --- the number of notes.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤1090 \le a_i \le 10^9) --- the base score of each note.

Each of the next nn lines contains two integers lil_i and rir_i (0≤li≤ri≤10180 \le l_i \le r_i \le 10^{18}) --- the target interval for the cumulative score after note ii.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

Output Format

For each test case, output a single integer --- the minimum number of item uses. If it is impossible, output −1-1.

2
3
6 2 8
8 16
16 32
28 36
2
0 0
1 1
0 0
4
-1

Hint

For the first test case in the sample, one feasible operation scheme is as follows: use the item 44 times on the interval [1,3][1,3]. The original base score sequence becomes [10,6,12][10,6,12], and the cumulative scores become [10,16,28][10,16,28], which satisfies the conditions. The total number of operations is 44. It can be proved that no scheme with fewer operations exists.

For the second test case in the sample, it can be proved that no feasible operation scheme exists.