Snow Boots

Find the minimum number of boot pairs Farmer John must discard, given a stack-ordered backpack and snow-depth and step-size limits, to walk from tile 1 to tile N.

Medium7Dynamic programmingArrayBrute forceImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

Winter has arrived on the farm, and with it the snow. A path of NN tiles runs from the farmhouse to the barn, numbered 11 through NN, and tile ii is covered by fif_i feet of snow.

Farmer John starts on tile 11 and has to reach tile NN to wake the cows. Tile 11 sits under the farmhouse roof and tile NN sits under the barn roof, so neither one holds any snow. To step on any other tile, Farmer John needs boots.

His foul-weather backpack holds BB pairs of boots, numbered 11 through BB. Some pairs are sturdier and some are nimbler: pair ii lets him step in snow at most sis_i feet deep, and lets him move at most did_i tiles forward in one step.

The boots are packed in a stack, so only the topmost pair is within reach. At any moment Farmer John can put on the topmost pair, which discards the pair he was wearing, or discard the topmost pair without wearing it, which exposes the pair below.

Farmer John changes boots only while standing on a tile. If that tile holds ff feet of snow, the pair he takes off and the pair he puts on must both withstand at least ff feet. A pair he discards without ever wearing it is free of this restriction.

Farmer John starts out wearing no boots. Find the smallest number of pairs he has to discard to reach the barn.

Input

The first line contains two integers NN and BB separated by a space (2N,B2502 \leq N, B \leq 250).

The second line contains NN integers separated by spaces. The iith of them is fif_i, the depth of snow on tile ii (0fi1090 \leq f_i \leq 10^9). It is guaranteed that f1=fN=0f_1 = f_N = 0.

Each of the next BB lines contains two integers separated by a space. Line i+2i+2 holds sis_i, the greatest snow depth pair ii can step in, and did_i, the greatest step size for pair ii (0si1090 \leq s_i \leq 10^9, 1diN11 \leq d_i \leq N-1).

The pairs are listed from the top of the backpack downward, so pair 11 is the topmost pair.

Output

Print one integer, the smallest number of pairs Farmer John has to discard. Reaching the barn is always possible.