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,diff,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]);
diff[i]:=C[i];
price[i]:=0;
end;
readln();
costototale:=0;
for i:=0 to K-1 do
begin
read(T[i],Q[i]); readln;
D:= diff[T[i]]-Q[i];
if D>=0 then begin diff[T[i]]:=D; price[T[i]]:=price[T[i]]+diff[T[i]]; end
else
begin
price[T[i]]:=price[T[i]]+diff[T[i]];
diff[T[i]]:=C[T[i]]-Q[i];
end;
write(price[T[i]]); writeln;
end;
for i:=0 to N-1 do costototale:=costototale+price[i];
writeln(costototale);
for i:=0 to N-1 do write(price[i],' '); writeln;
end.
cHJvZ3JhbSBzY29henplOyAKKCphbGdvcml0bW8gZ3JlZWR5OiBQZXJ0YW50bywgb2duaSBnaW9ybm8gaW4gY3VpIHJpY2V2aWFtbyBzcGF6emF0dXJhLCAqKQooKnNlIHNpYW1vIGNvc3RyZXR0aSBhIHN2dW90YXJlIGlsIGNlc3Rpbm8gYXR0dWFsZSwgbG8gZmFyZW1vLCBhbHRyaW1lbnRpICopCigqYWdnaXVuZ2VuZG8gYWxsYSBkaW1lbnNpb25lIGRlbCBjZXN0aW5vIGxhIHF1YW50aXTDoCBkaSBzcGF6emF0dXJhIHJhY2NvbHRhICopCigqbmVsIHBhc3NhZ2dpbyBjb3JyZW50ZS5BbCB0ZXJtaW5lIHN2dW90aWFtbyB0dXR0aSBpIGNhc3NvbmV0dGkuICopCigqRG9iYmlhbW8gc3Z1b3RhcmUgY2lhc2N1biBjb250ZW5pdG9yZSBpbCBwacO5IHRhcmRpIHBvc3NpYmlsZSBwZXIgcmlkdXJyZSBhbCBtaW5pbW8qKQooKmxhIHNwZXNhKikKY29uc3QKICAgIE1BWE4gPSAyMDAwMDA7Cgp7IGlucHV0IGRhdGEgfQp2YXIKICAgIE4sIEssIGkgIDogbG9uZ2ludDsKICAgIGNvc3RvdG90YWxlLCBEOmludDY0OwogICAgQyxULFEsZGlmZixwcmljZSAgICAgICAgOiBhcnJheVswLi5NQVhOLTFdIG9mIGludDY0OwoKYmVnaW4KewogICAgdW5jb21tZW50IHRoZSBmb2xsb3dpbmcgbGluZXMgaWYgeW91IHdhbnQgdG8gcmVhZC93cml0ZSBmcm9tIGZpbGVzCiAgICBhc3NpZ24oaW5wdXQsICAnaW5wdXQudHh0Jyk7ICByZXNldChpbnB1dCk7CiAgICBhc3NpZ24ob3V0cHV0LCAnb3V0cHV0LnR4dCcpOyByZXdyaXRlKG91dHB1dCk7Cn0KCiAgICB7IHJlYWQgbnVtYmVycyBOIGFuZCBLIGluIGEgc2luZ2xlIGxpbmUgfQogICAgcmVhZGxuKE4sIEspOwogICAgeyByZWFkIGFsbCBudW1iZXJzIENbaV0gaW4gdGhlIG5leHQgbGluZSB9CiAgICBmb3IgaTo9MCB0byBOLTEgZG8KICAgICAgICAgICAgICBiZWdpbgogICAgICAgICAgICAgICByZWFkKENbaV0pOwogICAgICAgICAgICAgICBkaWZmW2ldOj1DW2ldOwogICAgICAgICAgICAgICBwcmljZVtpXTo9MDsKICAgICAgICAgICAgIGVuZDsgIAogICAgcmVhZGxuKCk7CiAgICBjb3N0b3RvdGFsZTo9MDsKICAgIGZvciBpOj0wIHRvIEstMSBkbwogICAgICAgICAgICAgICAgICBiZWdpbgogICAgICAgICAgICAgICAgICAgIHJlYWQoVFtpXSxRW2ldKTsgcmVhZGxuOwogICAgICAgICAgICAgICAgICAgIEQ6PSBkaWZmW1RbaV1dLVFbaV07CiAgICAgICAgICAgICAgICAgICAgaWYgRD49MCB0aGVuIGJlZ2luIGRpZmZbVFtpXV06PUQ7IHByaWNlW1RbaV1dOj1wcmljZVtUW2ldXStkaWZmW1RbaV1dOyBlbmQKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJlZ2luCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHByaWNlW1RbaV1dOj1wcmljZVtUW2ldXStkaWZmW1RbaV1dOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBkaWZmW1RbaV1dOj1DW1RbaV1dLVFbaV07CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbmQ7IAogICAgICAgICAgICAgICAgICAgIHdyaXRlKHByaWNlW1RbaV1dKTsgd3JpdGVsbjsgICAgICAgICAgCiAgICAgICAgICAgICAgICAgIGVuZDsgICAgICAgICAgICAgIAogICAgZm9yIGk6PTAgdG8gTi0xIGRvIGNvc3RvdG90YWxlOj1jb3N0b3RvdGFsZStwcmljZVtpXTsKICAgIHdyaXRlbG4oY29zdG90b3RhbGUpOwogICAgZm9yIGk6PTAgdG8gTi0xIGRvIHdyaXRlKHByaWNlW2ldLCcgJyk7IHdyaXRlbG47CmVuZC4K