#P15628. [ICPC 2022 Jakarta R] Game Show

[ICPC 2022 Jakarta R] Game Show

Problem Description

You are hosting a game show. In your game show, there is a circular disk divided into NN regions, numbered from 11 to NN in clockwise order. For each region ii (1iN11 \leq i \leq N-1), region i+1i+1 is located to the next of region ii, and region 11 is located to the next of region NN.

There are QQ independent rounds. In each round, the player starts from region SS and the target is at region TT. For each ii such that 1iN1 \leq i \leq N, the player can move from region ii to region i+1i+1 (or to region 11 if i=Ni=N) with a penalty of AiA_i. Similarly, the player can move from region i+1i+1 (or from region 11 if i=Ni=N) to region ii with a penalty of BiB_i. Note that the penalty can be negative.

The goal of each round is to find the minimum total penalty required to reach the target. However, you noticed that it is possible for the player to abuse the game to reach the target with a penalty of -\infty. Such round is called flawed.

For each round, determine if the round is flawed or not. If the round is not flawed, determine the minimum penalty to reach the target.

Input Format

Input begins with two integers NN QQ (3N2000003 \leq N \leq 200\,000; 1Q2000001 \leq Q \leq 200\,000) representing the number of regions and the number of rounds, respectively.

The next line contains NN integers AiA_i (109Ai109-10^9 \leq A_i \leq 10^9) representing the penalty to move from region ii to region i+1i+1, or to region 11 if i=Ni=N. The next line contains NN integers BiB_i (109Bi109-10^9 \leq B_i \leq 10^9) representing the penalty to move from region i+1i+1, or from region 11 if i=Ni=N, to region ii.

Each of the next QQ lines contains two integers SS TT (1S,TN1 \leq S, T \leq N) representing the start region and target region of each round, respectively.

Output Format

For each round, if the round is flawed, then output flawed in a single line. Otherwise, output an integer in a single line, representing the minimum penalty to reach the target.

4 4
2 3 -4 3
1 2 7 -1
1 3
3 1
1 4
1 1
5
-1
-1
0
4 3
1 2 -3 4
4 -3 2 1
1 1
2 4
3 1
flawed
flawed
flawed
6 2
-6 8 -3 5 -9 4
9 -2 8 -4 12 -1
2 6
3 3
flawed
flawed

Hint

Explanation for the sample input/output #1

In round 11, the path 1231 \rightarrow 2 \rightarrow 3 has a penalty of 2+3=52 + 3 = 5.

In round 22, the path 3413 \rightarrow 4 \rightarrow 1 has a penalty of (4)+3=1(-4) + 3 = -1. This path has lesser penalty than path 3213 \rightarrow 2 \rightarrow 1, which has a penalty of 2+1=32 + 1 = 3.

In round 33, the path 141 \rightarrow 4 has a penalty of 1-1.

Explanation for the sample input/output #2

For all rounds, the player can go to region 22, then repeatedly travel back and forth in regions 22 and 33 to reduce the penalty by 11 infinitely many times.