program scoazze;
(*algoritmo greedy: Pertanto, ogni giorno in cui riceviamo spazzatura, *)
(*se siamo costretti a svuotare il cestino attuale, lo faremo, altrimenti *)
(*aggiungendo alla dimensione del cestino la quantità di spazzatura raccolta *)
(*nel passaggio corrente.Al termine svuotiamo tutti i cassonetti. *)
(*Dobbiamo svuotare ciascun contenitore il più tardi possibile per ridurre al minimo*)
(*la spesa*)
const
MAXN = 200000;
{ input data }
var
N, K, i : longint;
costototale, D:int64;
C,T,Q,bidone,price : array[0..MAXN-1] of int64;
begin
{
uncomment the following lines if you want to read/write from files
assign(input, 'input.txt'); reset(input);
assign(output, 'output.txt'); rewrite(output);
}
{ read numbers N and K in a single line }
readln(N, K);
{ read all numbers C[i] in the next line }
for i:=0 to N-1 do
begin
read(C[i]);
bidone[i]:=0;
price[i]:=0;
end;
readln();
costototale:=0;
for i:=0 to K-1 do
begin
read(T[i],Q[i]); readln;
if C[T[i]]-(bidone[T[i]]+Q[i])>=0 then bidone[T[i]]:=bidone[T[i]]+Q[i]
else
begin
costototale:=costototale+(C[T[i]]-bidone[T[i]]);
bidone[T[i]]:=Q[i];
end;
end;
for i:=0 to N-1 do costototale:=costototale+C[i]-bidone[i];
writeln(costototale);
end.
cHJvZ3JhbSBzY29henplOyAKKCphbGdvcml0bW8gZ3JlZWR5OiBQZXJ0YW50bywgb2duaSBnaW9ybm8gaW4gY3VpIHJpY2V2aWFtbyBzcGF6emF0dXJhLCAqKQooKnNlIHNpYW1vIGNvc3RyZXR0aSBhIHN2dW90YXJlIGlsIGNlc3Rpbm8gYXR0dWFsZSwgbG8gZmFyZW1vLCBhbHRyaW1lbnRpICopCigqYWdnaXVuZ2VuZG8gYWxsYSBkaW1lbnNpb25lIGRlbCBjZXN0aW5vIGxhIHF1YW50aXTDoCBkaSBzcGF6emF0dXJhIHJhY2NvbHRhICopCigqbmVsIHBhc3NhZ2dpbyBjb3JyZW50ZS5BbCB0ZXJtaW5lIHN2dW90aWFtbyB0dXR0aSBpIGNhc3Nv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AgICAgICAgICAgICBpZiBDW1RbaV1dLShiaWRvbmVbVFtpXV0rUVtpXSk+PTAgdGhlbiBiaWRvbmVbVFtpXV06PWJpZG9uZVtUW2ldXStRW2ldCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBiZWdpbgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY29zdG90b3RhbGU6PWNvc3RvdG90YWxlKyhDW1RbaV1dLWJpZG9uZVtUW2ldXSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBiaWRvbmVbVFtpXV06PVFbaV07CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZW5kOyAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICBlbmQ7IAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICBmb3IgaTo9MCB0byBOLTEgZG8gY29zdG90b3RhbGU6PWNvc3RvdG90YWxlK0NbaV0tYmlkb25lW2ldOwogICAgd3JpdGVsbihjb3N0b3RvdGFsZSk7CmVuZC4=