module IntMap = Map.Make (struct
  type t = int
  let compare = compare
end)

let add a s =
  IntMap.add a (1 + try IntMap.find a s with Not_found -> 0) s

let remove a s =
  match IntMap.find a s with
  | exception Not_found -> s
  | 1 -> IntMap.remove a s
  | n -> IntMap.add a (n - 1) s

let _ = Scanf.scanf "%d\n1 %d %d\n" @@ fun q a b ->
  Array.fold_left (fun (acc, (n, n', q, q', sum, sum')) ->
    let (m, _) = IntMap.max_binding q in
    function
    | None ->
        Printf.printf "%d %d\n" m
          (acc + (n * m - sum) + (sum' - n' * m));
        (acc, (n, n', q, q', sum, sum'))
    | Some (a, b) ->
        acc + b,
        if a < m then
          if n <= n'
          then (n + 1, n', add a q, q', sum + a, sum')
          else (n, n' + 1, add a (remove m q), add m q', sum + a - m, sum' + m)
        else
          if n' < n
          then (n, n' + 1, q, add a q', sum, sum' + a)
          else (n + 1, n', add m q, add a (remove m q'), sum + m, sum' + a - m))
  (b, (1, 0, IntMap.singleton a 1, IntMap.empty, a, 0)) @@
  Array.init (q - 1) @@ fun _ ->
    Scanf.scanf "%d " @@ function
      | 2 -> None
      | 1 -> Scanf.scanf "%d %d " @@ fun a b -> Some (a, b)
