atcoder#ARC105C. [ARC105C] Camels and Bridge

[ARC105C] Camels and Bridge

Score : 500500 points

Problem Statement

There are NN camels numbered 11 through NN.

The weight of Camel ii is wiw_i.

You will arrange the camels in a line and make them cross a bridge consisting of MM parts.

Before they cross the bridge, you can choose their order in the line - it does not have to be Camel 11, 22, \ldots, NN from front to back - and specify the distance between each adjacent pair of camels to be any non-negative real number. The camels will keep the specified distances between them while crossing the bridge.

The ii-th part of the bridge has length lil_i and weight capacity viv_i. If the sum of the weights of camels inside a part (excluding the endpoints) exceeds viv_i, the bridge will collapse.

Determine whether it is possible to make the camels cross the bridge without it collapsing. If it is possible, find the minimum possible distance between the first and last camels in the line in such a case.

It can be proved that the answer is always an integer, so print an integer.

Constraints

  • All values in input are integers.
  • 2N82 \leq N \leq 8
  • 1M1051 \leq M \leq 10^5
  • 1wi,li,vi1081 \leq w_i,l_i,v_i \leq 10^8

Input

Input is given from Standard Input in the following format:

NN MM

w1w_1 w2w_2 \cdots wNw_N

l1l_1 v1v_1

\vdots

lMl_M vMv_M

Output

If the bridge will unavoidably collapse when the camels cross the bridge, print -1. Otherwise, print the minimum possible distance between the first and last camels in the line when the camels cross the bridge without it collapsing.

3 2
1 4 2
10 4
2 6
10
  • It is possible to make the camels cross the bridge without it collapsing by, for example, arranging them in the order 11, 33, 22 from front to back, and setting the distances between them to be 00, 1010.- For Part 11 of the bridge, there are moments when only Camel 11 and 33 are inside the part and moments when only Camel 22 is inside the part. In both cases, the sum of the weights of camels does not exceed 44 - the weight capacity of Part 11 - so there is no collapse.
    • For Part 22 of the bridge, there are moments when only Camel 11 and 33 are inside the part and moments when only Camel 22 is inside the part. In both cases, the sum of the weights of camels does not exceed 66 - the weight capacity of Part 22 - so there is no collapse.
  • For Part 11 of the bridge, there are moments when only Camel 11 and 33 are inside the part and moments when only Camel 22 is inside the part. In both cases, the sum of the weights of camels does not exceed 44 - the weight capacity of Part 11 - so there is no collapse.
  • For Part 22 of the bridge, there are moments when only Camel 11 and 33 are inside the part and moments when only Camel 22 is inside the part. In both cases, the sum of the weights of camels does not exceed 66 - the weight capacity of Part 22 - so there is no collapse.
  • Note that the distance between two camels may be 00 and that camels on endpoints of a part are not considered to be inside the part.
2 1
12 345
1 1
-1
  • Print -1 if the bridge will unavoidably collapse.
8 1
1 1 1 1 1 1 1 1
100000000 1
700000000
8 20
57 806 244 349 608 849 513 857
778 993
939 864
152 984
308 975
46 860
123 956
21 950
850 876
441 899
249 949
387 918
34 965
536 900
875 889
264 886
583 919
88 954
845 869
208 963
511 975
3802