Under construction

Still being built. Things may change

Camp Circular Bus

Interview

Time limit1sMemory limit1024 MB

Summary
Find when the last soldier alights from a circular bus that never stops, where each soldier boards at the first bus pass after their wait starts.
Level

Medium4 of 10

Topics
Implementation, Math
Solved
No attempts yet

Problem

There are NN boarding and alighting stops in the camp, numbered from 11 to NN. On a clear morning, the driver starts the bus again to help soldiers move around the camp.

The driver's bus is called the camp circular bus and has the following features.

  • The camp circular bus leaves stop 11 at time 00, visits stops 22, 33, ⋯\cdots, NN in order, then returns to stop 11. It keeps repeating this route.
  • After departure, the bus cannot stop until all MM soldiers have boarded and alighted.
  • The bus has no capacity limit. A soldier who wants to board can always get on.

A soldier starts waiting for the bus at a time, boards the moment the bus reaches the boarding stop, and alights the moment the bus reaches the alighting stop.

The driver helped all MM soldiers move and finished work as soon as the last soldier alighted. Find the time when the driver finished work.

Input

The first line contains the number of stops NN and the number of soldiers MM, separated by a space. (2≤N≤100 000;(2 \leq N \leq 100\,000; 1≤M≤100 000)1 \leq M \leq 100\,000)

The second line contains integers tit_i, separated by spaces, which is the time to travel from stop ii to stop i+1i+1. Here tNt_N is the time to return from stop NN to stop 11. (1≤i≤N;(1 \leq i \leq N; 1≤ti≤1 000)1 \leq t_i \leq 1\,000)

From the third line through line 2+M2+M, each line gives one soldier: the boarding stop pip_i, the alighting stop rir_i, and the time cic_i when the soldier started waiting for the bus at the boarding stop, separated by spaces. (1≤i≤M;(1 \leq i \leq M; 1≤pi,ri≤N;1 \leq p_i, r_i \leq N; pi≠ri;p_i \neq r_i; 0≤ci≤109)0 \leq c_i \leq 10^9)

Output

Print one line with the time when the driver finished work.

Examples1

  1. Example 1

    Input
    5 7
    1 3 2 4 3
    1 2 0
    2 5 1
    3 4 2
    3 1 7
    5 4 10
    1 4 12
    4 3 4
    
    Expected output
    26