open Scanf open Printf let readint () = scanf " %d" (fun x -> x) type rmq = { dat : int array; n : int } module RMQ = struct let make (n : int) : rmq = let rec f x = if x >= n then x else f (x * 2) in { dat = Array.make (2 * f n) min_int; n = f n } let update (rmq : rmq) (k : int) (v : int) = rmq.dat.(k + rmq.n) <- v; let rec f k = if k >= 1 then begin rmq.dat.(k) <- max rmq.dat.(k * 2) rmq.dat.(k * 2 + 1); f (k lsr 1) end in f ((k + rmq.n) lsr 1) let query (rmq : rmq) (l : int) (r : int) = let rec f l r ans = if l <= r then f ((l + l land 1) lsr 1) ((r - r land 1) lsr 1) @@ match (l land 1, r land 1) with | (0, 0) -> rmq.dat.(r) | (1, 0) -> max rmq.dat.(l) rmq.dat.(r) | (0, 1) -> min_int | _ -> rmq.dat.(l) else ans in f (l + rmq.n) (r + rmq.n - 1) min_int end;; let () = let n = readint () in let a = Array.init n (fun _ -> readint ()) in let q = readint () in let rmq = RMQ.make (n - 24 + 1) in let calc i = Array.sub a i 24 |> Array.fold_left (+) 0 in for i = 0 to n - 24 do RMQ.update rmq i (calc i); done; for _ = 0 to q - 1 do let t = readint () - 1 and v = readint () in a.(t) <- v; for i = min (n - 24) t downto max 0 (t - 23) do RMQ.update rmq i (calc i); done; printf "%d\n" (RMQ.query rmq 0 (n - 24 + 1)) done;