Game Show

시간 제한2초메모리 제한1024 MB

요약
방향 간선 가중치가 있는 원형 그래프에서 S에서 T까지의 최단 비용을 구하거나, 음수 사이클 때문에 비용이 무한히 작아질 수 있으면 flawed를 출력한다.
난이도

어려움10점 중 8점

유형
최단 경로, 그래프, 누적 합, 동적 계획법
정답자
아직 제출이 없습니다

문제

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 (1≤i≤N−11 ≤ i ≤ 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 1≤i≤N1 ≤ i ≤ 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 A_iA\_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 B_iB\_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 −∞-∞. 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 begins with two integers NN QQ (3≤N≤200,0003 ≤ N ≤ 200\\, 000; 1≤Q≤200,0001 ≤ Q ≤ 200\\, 000) representing the number of regions and the number of rounds, respectively.

The next line contains NN integers A_iA\_i (−109≤A_i≤109-10^9 ≤ A\_i ≤ 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 B_iB\_i (−109≤B_i≤109-10^9 ≤ B\_i ≤ 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 (1≤S,T≤N1 ≤ S, T ≤ N) representing the start region and target region of each round, respectively.

출력

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.

예제3

  1. 예제 1

    입력
    4 4
    2 3 -4 3
    1 2 7 -1
    1 3
    3 1
    1 4
    1 1
    
    예상 출력
    5
    -1
    -1
    0
    
  2. 예제 2

    입력
    4 3
    1 2 -3 4
    4 -3 2 1
    1 1
    2 4
    3 1
    
    예상 출력
    flawed
    flawed
    flawed
    
  3. 예제 3

    입력
    6 2
    -6 8 -3 5 -9 4
    9 -2 8 -4 12 -1
    2 6
    3 3
    
    예상 출력
    flawed
    flawed