#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
void *operator new(std::size_t sz) { static char *b, *e; if (b + sz > e) { b = (char *) std::malloc(sz > (1 << 30) ? sz : (1 << 30)); e = b + (sz > (1 << 30) ? sz : (1 << 30)); } return e -= sz; }
struct $t_bsl_Bool { enum { $e_bsl_False, $e_bsl_True } T; void* ptr; };
struct $d_bsl_False { };
struct $d_bsl_True { };
struct $t_bsl_IO { enum { $e_bsl_Pure, $e_bsl_Free } T; void* ptr; };
struct $d_bsl_Pure { void* d0; };
struct $d_bsl_Free { void* d0; };
struct $t_bsl_IOImpl { enum { $e_bsl_Read, $e_bsl_Write } T; void* ptr; };
struct $d_bsl_Read { void* d0; };
struct $d_bsl_Write { void* d0; void* d1; };
typedef  void  $t_bsl_Int;
struct $t_bsl_List { enum { $e_bsl_Nil, $e_bsl_Cons } T; void* ptr; };
struct $d_bsl_Nil { };
struct $d_bsl_Cons { void* d0; void* d1; };
struct $t_bsl_Maybe { enum { $e_bsl_Just, $e_bsl_Nothing } T; void* ptr; };
struct $d_bsl_Just { void* d0; };
struct $d_bsl_Nothing { };
struct $t_bsl_Pair { enum { $e_bsl_Pair } T; void* ptr; };
struct $d_bsl_Pair { void* d0; void* d1; };
struct $t_bsl_Unit { enum { $e_bsl_Unit } T; void* ptr; };
struct $d_bsl_Unit { };
int main() { [=]() -> void* { void* $tmp_bsl_tmp = new $t_bsl_Unit { $t_bsl_Unit::$e_bsl_Unit, new $d_bsl_Unit{ } };  void* $v_bsl_Unit = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_arg0) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_arg1) -> void* { return new $t_bsl_Pair { $t_bsl_Pair::$e_bsl_Pair, new $d_bsl_Pair{ $v_bsl_arg0, $v_bsl_arg1 } }; }); });  void* $v_bsl_Pair = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new $t_bsl_Maybe { $t_bsl_Maybe::$e_bsl_Nothing, new $d_bsl_Nothing{ } };  void* $v_bsl_Nothing = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_arg0) -> void* { return new $t_bsl_Maybe { $t_bsl_Maybe::$e_bsl_Just, new $d_bsl_Just{ $v_bsl_arg0 } }; });  void* $v_bsl_Just = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_arg0) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_arg1) -> void* { return new $t_bsl_List { $t_bsl_List::$e_bsl_Cons, new $d_bsl_Cons{ $v_bsl_arg0, $v_bsl_arg1 } }; }); });  void* $v_bsl_Cons = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new $t_bsl_List { $t_bsl_List::$e_bsl_Nil, new $d_bsl_Nil{ } };  void* $v_bsl_Nil = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_arg0) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_arg1) -> void* { return new $t_bsl_IOImpl { $t_bsl_IOImpl::$e_bsl_Write, new $d_bsl_Write{ $v_bsl_arg0, $v_bsl_arg1 } }; }); });  void* $v_bsl_Write = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_arg0) -> void* { return new $t_bsl_IOImpl { $t_bsl_IOImpl::$e_bsl_Read, new $d_bsl_Read{ $v_bsl_arg0 } }; });  void* $v_bsl_Read = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_arg0) -> void* { return new $t_bsl_IO { $t_bsl_IO::$e_bsl_Free, new $d_bsl_Free{ $v_bsl_arg0 } }; });  void* $v_bsl_Free = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_arg0) -> void* { return new $t_bsl_IO { $t_bsl_IO::$e_bsl_Pure, new $d_bsl_Pure{ $v_bsl_arg0 } }; });  void* $v_bsl_Pure = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new $t_bsl_Bool { $t_bsl_Bool::$e_bsl_True, new $d_bsl_True{ } };  void* $v_bsl_True = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new $t_bsl_Bool { $t_bsl_Bool::$e_bsl_False, new $d_bsl_False{ } };  void* $v_bsl_False = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_f) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_IOImpl*)($tmp_bsl_tmp))->T) { case $t_bsl_IOImpl::$e_bsl_Write: {void* $v_bsl_s = (($d_bsl_Write*)((($t_bsl_IOImpl*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_k = (($d_bsl_Write*)((($t_bsl_IOImpl*)($tmp_bsl_tmp))->ptr))->d1; return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Write)))($v_bsl_s))))((*((std::function<void*(void*)>*)($v_bsl_f)))($v_bsl_k)); } case $t_bsl_IOImpl::$e_bsl_Read: {void* $v_bsl_k = (($d_bsl_Read*)((($t_bsl_IOImpl*)($tmp_bsl_tmp))->ptr))->d0; return (*((std::function<void*(void*)>*)($v_bsl_Read)))(new std::function<void*(void*)>([=](void* $v_bsl_s) -> void* { return (*((std::function<void*(void*)>*)($v_bsl_f)))((*((std::function<void*(void*)>*)($v_bsl_k)))($v_bsl_s)); })); } } }(); }); });  void* $v_bsl_fmap = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_Pure;  void* $v_bsl_return = $tmp_bsl_tmp; return [=]() -> void* { void *$v_bsl_bind = new std::function<void*(void*)>(); { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_f) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_IO*)($tmp_bsl_tmp))->T) { case $t_bsl_IO::$e_bsl_Pure: {void* $v_bsl_x = (($d_bsl_Pure*)((($t_bsl_IO*)($tmp_bsl_tmp))->ptr))->d0; return (*((std::function<void*(void*)>*)($v_bsl_f)))($v_bsl_x); } case $t_bsl_IO::$e_bsl_Free: {void* $v_bsl_x = (($d_bsl_Free*)((($t_bsl_IO*)($tmp_bsl_tmp))->ptr))->d0; return (*((std::function<void*(void*)>*)($v_bsl_Free)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_fmap)))(new std::function<void*(void*)>([=](void* $v_bsl_y) -> void* { return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_bind)))($v_bsl_y))))($v_bsl_f); })))))($v_bsl_x)); } } }(); }); }); std::memcpy($v_bsl_bind, $tmp_bsl_tmp, sizeof (std::function<void*(void*)>)); } return [=]() -> void* { void* $tmp_bsl_tmp = (*((std::function<void*(void*)>*)($v_bsl_Free)))((*((std::function<void*(void*)>*)($v_bsl_Read)))(new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return (*((std::function<void*(void*)>*)($v_bsl_return)))($v_bsl_x); })));  void* $v_bsl_getInt = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return (*((std::function<void*(void*)>*)($v_bsl_Free)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Write)))($v_bsl_x))))((*((std::function<void*(void*)>*)($v_bsl_return)))($v_bsl_Unit))); });  void* $v_bsl_putInt = $tmp_bsl_tmp; return [=]() -> void* { void *$v_bsl_runIO = new std::function<void*(void*)>(); { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_IO*)($tmp_bsl_tmp))->T) { case $t_bsl_IO::$e_bsl_Pure: {void* $v_bsl_x = (($d_bsl_Pure*)((($t_bsl_IO*)($tmp_bsl_tmp))->ptr))->d0; return $v_bsl_x; } case $t_bsl_IO::$e_bsl_Free: {void* $v_bsl_x = (($d_bsl_Free*)((($t_bsl_IO*)($tmp_bsl_tmp))->ptr))->d0; return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_IOImpl*)($tmp_bsl_tmp))->T) { case $t_bsl_IOImpl::$e_bsl_Write: {void* $v_bsl_c = (($d_bsl_Write*)((($t_bsl_IOImpl*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_x = (($d_bsl_Write*)((($t_bsl_IOImpl*)($tmp_bsl_tmp))->ptr))->d1; return [=]() -> void* { void* $tmp_bsl_tmp =  (std::printf("%d\n", (std::intptr_t) $v_bsl_c), nullptr) ;  void* $v_bsl__ = $tmp_bsl_tmp; return (*((std::function<void*(void*)>*)($v_bsl_runIO)))($v_bsl_x); } (); } case $t_bsl_IOImpl::$e_bsl_Read: {void* $v_bsl_g = (($d_bsl_Read*)((($t_bsl_IOImpl*)($tmp_bsl_tmp))->ptr))->d0; return [=]() -> void* { void* $tmp_bsl_tmp =  [=]() -> void* {
        int x;
        if (std::scanf("%d", &x) == 1)
          return (*((std::function<void*(void*)>*)$v_bsl_Just))((void*) (std::intptr_t) x);
        else
          return $v_bsl_Nothing;
      }() ;  void* $v_bsl_x = $tmp_bsl_tmp; return (*((std::function<void*(void*)>*)($v_bsl_runIO)))((*((std::function<void*(void*)>*)($v_bsl_g)))($v_bsl_x)); } (); } } }(); } } }(); }); std::memcpy($v_bsl_runIO, $tmp_bsl_tmp, sizeof (std::function<void*(void*)>)); } return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_Bool*)($tmp_bsl_tmp))->T) { case $t_bsl_Bool::$e_bsl_True: { return $v_bsl_False; } case $t_bsl_Bool::$e_bsl_False: { return $v_bsl_True; } } }(); });  void* $v_bsl_bnot = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_Bool*)($tmp_bsl_tmp))->T) { case $t_bsl_Bool::$e_bsl_True: { return new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return $v_bsl_x; }); } case $t_bsl_Bool::$e_bsl_False: { return new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return $v_bsl_False; }); } } }(); });  void* $v_bsl_band = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_b) -> void* { return  (void*) (((std::intptr_t) $v_bsl_a) + ((std::intptr_t) $v_bsl_b)) ; }); });  void* $v_bsl_add = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return  (void*) -((std::intptr_t) $v_bsl_a) ; });  void* $v_bsl_neg = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_b) -> void* { return  (void*) (((std::intptr_t) $v_bsl_a) - ((std::intptr_t) $v_bsl_b)) ; }); });  void* $v_bsl_sub = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_b) -> void* { return  (void*) (((std::intptr_t) $v_bsl_a) * ((std::intptr_t) $v_bsl_b)) ; }); });  void* $v_bsl_mul = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_b) -> void* { return  (void*) (((std::intptr_t) $v_bsl_a) / ((std::intptr_t) $v_bsl_b)) ; }); });  void* $v_bsl_div = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_b) -> void* { return  (void*) (((std::intptr_t) $v_bsl_a) % ((std::intptr_t) $v_bsl_b)) ; }); });  void* $v_bsl_mod = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_b) -> void* { return  (((std::intptr_t) $v_bsl_a) < ((std::intptr_t) $v_bsl_b))?$v_bsl_True:$v_bsl_False ; }); });  void* $v_bsl_less = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return  ((std::intptr_t) $v_bsl_a) == 0?$v_bsl_True:$v_bsl_False ; });  void* $v_bsl_eq0 = $tmp_bsl_tmp; return [=]() -> void* { void *$v_bsl_gcd = new std::function<void*(void*)>(); { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_b) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = (*((std::function<void*(void*)>*)($v_bsl_eq0)))($v_bsl_a); switch ((($t_bsl_Bool*)($tmp_bsl_tmp))->T) { case $t_bsl_Bool::$e_bsl_True: { return $v_bsl_b; } case $t_bsl_Bool::$e_bsl_False: { return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_gcd)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_mod)))($v_bsl_b))))($v_bsl_a)))))($v_bsl_a); } } }(); }); }); std::memcpy($v_bsl_gcd, $tmp_bsl_tmp, sizeof (std::function<void*(void*)>)); } return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_y) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_less)))($v_bsl_x))))($v_bsl_y); switch ((($t_bsl_Bool*)($tmp_bsl_tmp))->T) { case $t_bsl_Bool::$e_bsl_True: { return $v_bsl_y; } case $t_bsl_Bool::$e_bsl_False: { return $v_bsl_x; } } }(); }); });  void* $v_bsl_max = $tmp_bsl_tmp; return [=]() -> void* { void *$v_bsl_concat = new std::function<void*(void*)>(); { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_a) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_b) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_a; switch ((($t_bsl_List*)($tmp_bsl_tmp))->T) { case $t_bsl_List::$e_bsl_Nil: { return $v_bsl_b; } case $t_bsl_List::$e_bsl_Cons: {void* $v_bsl_x = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_xs = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d1; return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Cons)))($v_bsl_x))))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_concat)))($v_bsl_xs))))($v_bsl_b)); } } }(); }); }); std::memcpy($v_bsl_concat, $tmp_bsl_tmp, sizeof (std::function<void*(void*)>)); } return [=]() -> void* { void *$v_bsl_filter = new std::function<void*(void*)>(); { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_list) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_f) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_list; switch ((($t_bsl_List*)($tmp_bsl_tmp))->T) { case $t_bsl_List::$e_bsl_Nil: { return $v_bsl_Nil; } case $t_bsl_List::$e_bsl_Cons: {void* $v_bsl_x = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_xs = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d1; return [=]() -> void* { void* $tmp_bsl_tmp = (*((std::function<void*(void*)>*)($v_bsl_f)))($v_bsl_x); switch ((($t_bsl_Bool*)($tmp_bsl_tmp))->T) { case $t_bsl_Bool::$e_bsl_True: { return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Cons)))($v_bsl_x))))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_filter)))($v_bsl_xs))))($v_bsl_f)); } case $t_bsl_Bool::$e_bsl_False: { return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_filter)))($v_bsl_xs))))($v_bsl_f); } } }(); } } }(); }); }); std::memcpy($v_bsl_filter, $tmp_bsl_tmp, sizeof (std::function<void*(void*)>)); } return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_less) -> void* { return [=]() -> void* { void *$v_bsl_sortLess = new std::function<void*(void*)>(); { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_list) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_list; switch ((($t_bsl_List*)($tmp_bsl_tmp))->T) { case $t_bsl_List::$e_bsl_Nil: { return $v_bsl_Nil; } case $t_bsl_List::$e_bsl_Cons: {void* $v_bsl_x = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_xs = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d1; return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_concat)))((*((std::function<void*(void*)>*)($v_bsl_sortLess)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_filter)))($v_bsl_xs))))(new std::function<void*(void*)>([=](void* $v_bsl_y) -> void* { return (*((std::function<void*(void*)>*)($v_bsl_bnot)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_less)))($v_bsl_x))))($v_bsl_y)); })))))))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Cons)))($v_bsl_x))))((*((std::function<void*(void*)>*)($v_bsl_sortLess)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_filter)))($v_bsl_xs))))((*((std::function<void*(void*)>*)($v_bsl_less)))($v_bsl_x))))); } } }(); }); std::memcpy($v_bsl_sortLess, $tmp_bsl_tmp, sizeof (std::function<void*(void*)>)); } return $v_bsl_sortLess; } (); });  void* $v_bsl_sort = $tmp_bsl_tmp; return [=]() -> void* { void *$v_bsl_getList = new $t_bsl_IO(); { void* $tmp_bsl_tmp = (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_bind)))($v_bsl_getInt))))(new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_Maybe*)($tmp_bsl_tmp))->T) { case $t_bsl_Maybe::$e_bsl_Just: {void* $v_bsl_x = (($d_bsl_Just*)((($t_bsl_Maybe*)($tmp_bsl_tmp))->ptr))->d0; return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_bind)))($v_bsl_getList))))(new std::function<void*(void*)>([=](void* $v_bsl_xs) -> void* { return (*((std::function<void*(void*)>*)($v_bsl_return)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Cons)))($v_bsl_x))))($v_bsl_xs)); })); } case $t_bsl_Maybe::$e_bsl_Nothing: { return (*((std::function<void*(void*)>*)($v_bsl_return)))($v_bsl_Nil); } } }(); })); std::memcpy($v_bsl_getList, $tmp_bsl_tmp, sizeof ($t_bsl_IO)); } return [=]() -> void* { void *$v_bsl_putList = new std::function<void*(void*)>(); { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_list) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_list; switch ((($t_bsl_List*)($tmp_bsl_tmp))->T) { case $t_bsl_List::$e_bsl_Nil: { return (*((std::function<void*(void*)>*)($v_bsl_return)))($v_bsl_Unit); } case $t_bsl_List::$e_bsl_Cons: {void* $v_bsl_x = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_xs = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d1; return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_bind)))((*((std::function<void*(void*)>*)($v_bsl_putInt)))($v_bsl_x)))))(new std::function<void*(void*)>([=](void* $v_bsl__) -> void* { return (*((std::function<void*(void*)>*)($v_bsl_putList)))($v_bsl_xs); })); } } }(); }); std::memcpy($v_bsl_putList, $tmp_bsl_tmp, sizeof (std::function<void*(void*)>)); } return [=]() -> void* { void *$v_bsl_toList = new std::function<void*(void*)>(); { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_List*)($tmp_bsl_tmp))->T) { case $t_bsl_List::$e_bsl_Nil: { return $v_bsl_Nil; } case $t_bsl_List::$e_bsl_Cons: {void* $v_bsl_h = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_t = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d1; return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_t; switch ((($t_bsl_List*)($tmp_bsl_tmp))->T) { case $t_bsl_List::$e_bsl_Cons: {void* $v_bsl_h_ = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_t_ = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d1; return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Cons)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Pair)))($v_bsl_h))))($v_bsl_h_)))))((*((std::function<void*(void*)>*)($v_bsl_toList)))($v_bsl_t_)); } } }(); } } }(); }); std::memcpy($v_bsl_toList, $tmp_bsl_tmp, sizeof (std::function<void*(void*)>)); } return [=]() -> void* { void* $tmp_bsl_tmp =  (void*)0 ;  void* $v_bsl_zero = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_lst) -> void* { return [=]() -> void* { void *$v_bsl_getAns_ = new std::function<void*(void*)>(); { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_l) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_unfinishedTask) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_currentTime) -> void* { return new std::function<void*(void*)>([=](void* $v_bsl_maxTask) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_l; switch ((($t_bsl_List*)($tmp_bsl_tmp))->T) { case $t_bsl_List::$e_bsl_Nil: { return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Pair)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_add)))($v_bsl_currentTime))))($v_bsl_unfinishedTask)))))($v_bsl_maxTask); } case $t_bsl_List::$e_bsl_Cons: {void* $v_bsl_h = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_xs = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d1; return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_h; switch ((($t_bsl_Pair*)($tmp_bsl_tmp))->T) { case $t_bsl_Pair::$e_bsl_Pair: {void* $v_bsl_t = (($d_bsl_Pair*)((($t_bsl_Pair*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_c = (($d_bsl_Pair*)((($t_bsl_Pair*)($tmp_bsl_tmp))->ptr))->d1; return [=]() -> void* { void* $tmp_bsl_tmp = (*((std::function<void*(void*)>*)($v_bsl_eq0)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_sub)))($v_bsl_t))))($v_bsl_currentTime)); switch ((($t_bsl_Bool*)($tmp_bsl_tmp))->T) { case $t_bsl_Bool::$e_bsl_True: { return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_getAns_)))($v_bsl_xs))))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_add)))($v_bsl_unfinishedTask))))($v_bsl_c)))))($v_bsl_currentTime))))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_max)))($v_bsl_maxTask))))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_add)))($v_bsl_unfinishedTask))))($v_bsl_c))); } case $t_bsl_Bool::$e_bsl_False: { return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_getAns_)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Cons)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_Pair)))($v_bsl_t))))($v_bsl_c)))))($v_bsl_xs)))))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_max)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_add)))((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_sub)))($v_bsl_unfinishedTask))))($v_bsl_t)))))($v_bsl_currentTime)))))($v_bsl_zero)))))($v_bsl_t))))($v_bsl_maxTask); } } }(); } } }(); } } }(); }); }); }); }); std::memcpy($v_bsl_getAns_, $tmp_bsl_tmp, sizeof (std::function<void*(void*)>)); } return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_getAns_)))($v_bsl_lst))))($v_bsl_zero))))($v_bsl_zero))))($v_bsl_zero); } (); });  void* $v_bsl_getAns = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_Pair*)($tmp_bsl_tmp))->T) { case $t_bsl_Pair::$e_bsl_Pair: {void* $v_bsl_f = (($d_bsl_Pair*)((($t_bsl_Pair*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl__ = (($d_bsl_Pair*)((($t_bsl_Pair*)($tmp_bsl_tmp))->ptr))->d1; return $v_bsl_f; } } }(); });  void* $v_bsl_fst = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_Pair*)($tmp_bsl_tmp))->T) { case $t_bsl_Pair::$e_bsl_Pair: {void* $v_bsl__ = (($d_bsl_Pair*)((($t_bsl_Pair*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_s = (($d_bsl_Pair*)((($t_bsl_Pair*)($tmp_bsl_tmp))->ptr))->d1; return $v_bsl_s; } } }(); });  void* $v_bsl_snd = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = new std::function<void*(void*)>([=](void* $v_bsl_x) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = $v_bsl_x; switch ((($t_bsl_List*)($tmp_bsl_tmp))->T) { case $t_bsl_List::$e_bsl_Cons: {void* $v_bsl__ = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d0;void* $v_bsl_t = (($d_bsl_Cons*)((($t_bsl_List*)($tmp_bsl_tmp))->ptr))->d1; return $v_bsl_t; } } }(); });  void* $v_bsl_tail = $tmp_bsl_tmp; return [=]() -> void* { void* $tmp_bsl_tmp = (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_bind)))($v_bsl_getList))))(new std::function<void*(void*)>([=](void* $v_bsl_numbers) -> void* { return [=]() -> void* { void* $tmp_bsl_tmp = (*((std::function<void*(void*)>*)($v_bsl_getAns)))((*((std::function<void*(void*)>*)($v_bsl_toList)))((*((std::function<void*(void*)>*)($v_bsl_tail)))($v_bsl_numbers)));  void* $v_bsl_ans = $tmp_bsl_tmp; return (*((std::function<void*(void*)>*)((*((std::function<void*(void*)>*)($v_bsl_bind)))((*((std::function<void*(void*)>*)($v_bsl_putInt)))((*((std::function<void*(void*)>*)($v_bsl_fst)))($v_bsl_ans))))))(new std::function<void*(void*)>([=](void* $v_bsl__) -> void* { return (*((std::function<void*(void*)>*)($v_bsl_putInt)))((*((std::function<void*(void*)>*)($v_bsl_snd)))($v_bsl_ans)); })); } (); }));  void* $v_bsl_main = $tmp_bsl_tmp; return (*((std::function<void*(void*)>*)($v_bsl_runIO)))($v_bsl_main); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); } (); }


#include<iostream>
#include<algorithm>
#include<queue>
#include<utility>
#include<vector>

using namespace std;

typedef pair<int,int > pii;

int main()
{
	int n,msg=0,inter=0;
	cin>>n;
	pii arr[n+1];
	for (int i = 0; i < n; ++i)
	{
		int x,y;
		cin>>x>>y;
		if(i!=0)
		{
			arr[i]=make_pair(x,max(x+y,arr[i-1].second + y ));
			inter=max(inter - (x- arr[i-1].first),0) + y;
		}
		else
		{
			arr[i]=make_pair(x,x+y);
			inter+=y;
		}
		msg=max(inter,msg);
	}
	cout<< arr[n-1].second <<" " << msg<<"\n";
	return 0;
}


#include <bits/stdc++.h>
#include <fstream>

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using namespace std;

double Fib(double x,double z);
string BinDec(ull x);
string StringInt(ll x);
ull StringInt(string x);
ull BinDec (string x);
ull POWMOD (ull x,ull y,ull mod);
ull POWE(long long , long long);

string b(int x)
{
    int z=128;
    string ans;
    while(1)
    {
        if((x&z)==0)ans+='0';
        else ans+='1';
        if(z==0)break;
        z/=2;

    }
    reverse(ans.begin(),ans.end());
    return ans;
}

int x[2000200]={};

int main()
{

    ios::sync_with_stdio(false);
    cin.tie(0);

    int i,j,a,b,y,z=0,maxi=0,s=0;
    cin>>y;
    for(i=0;i<y;i++)
    {
        cin>>a>>b;
        x[a]=b;
        z=max(z,a);
    }
    i=1;
    while(1)
    {
        if(s==0&&z<i)break;
        if(s>0)s--;
        if(i<2000000)
        s+=x[i];
        if(s>maxi)
            maxi=s;
        i++;
    }
    cout<<i-1<<' '<<maxi;

    return 0;
}

double Fib(double x,double z){
    int i=0;
    double f=x,r=x,t;
    for(i=2;i<z;i++)
    {
        t=f;
        f+=r;
        r=t;
    }
    return f;

}
string BinDec( ull x){
    string z;
    while(1)
    {
        z+=((x%3)+'0');
        x/=3;
        if(x==0)
            break;
    }
    while(1)
    {
        if(z.size()==30)
            break;
        z+='0';
    }
    reverse(z.begin(),z.end());
    return z;
}
ull BinDec(string x){
    long long i;
    unsigned long long z=1,a=0;
    for(i=x.size()-1;i>=0;i--)
    {
        if(x[i]=='1')
            a+=z;
        if(x[i]=='2')
            a+=(2*z);
        z*=3;
    }
    return a;
}
ull StringInt(string x){
   ull z=0,i;
   for(i=0;i<x.size();i++)
   {
       z*=2;
       z+=(x[i]-'0');
   }
   return z;
}
string StringInt(ll x){
    string z;
    while(x!=0)
    {
        z+=(x%10+'0');
        x/=10;
    }
    reverse(z.begin(),z.end());
    return z;
}
ull POWMOD (ull base,ull exp,ull mod){
    ull res=1;
    while(exp>0){
        if(exp%2==1)
            res=(res*base);
            res%=mod;
        exp/=2;
        base=(base*base);
        base%=mod;
    }
return res%mod;
}
ull POWE(long long base,long long exp){
    ull res=1;
    while(exp>0){
        if(exp%2==1)
            res=(res*base);
        exp/=2;
        base=(base*base);
    }
return res;

}

/*
int s=0,c[1553][1553]={},z;
char a[1550][1550]={};
void lol(char m,int i,int j,int & s)
{

    if(c[i][j+1]==0&&a[i][j+1]==m)
    {
        s++;
        c[i][j+1]=1;
        lol(m,i,j+1,s);
    }
    if(c[i][j-1]==0&&a[i][j-1]==m)
    {
        s++;
        c[i][j-1]=1;
        lol(m,i,j-1,s);
    }
    if(c[i-1][j]==0&&a[i-1][j]==m)
    {
        s++;
        c[i-1][j]=1;
        lol(m,i-1,j,s);
    }
    if(c[i+1][j]==0&&a[i+1][j]==m)
    {
        s++;
        c[i+1][j]=1;
        lol(m,i+1,j,s);
    }
    if(z!=0)
    {
        z--;
        a[i][j]='X';
    }
    return;
}*/


#include <bits/stdc++.h>
#include <fstream>

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using namespace std;

double Fib(double x,double z);
string BinDec(ull x);
string StringInt(ll x);
ull StringInt(string x);
ull BinDec (string x);
ull POWMOD (ull x,ull y,ull mod);
ull POWE(long long , long long);

string b(int x)
{
    int z=128;
    string ans;
    while(1)
    {
        if((x&z)==0)ans+='0';
        else ans+='1';
        if(z==0)break;
        z/=2;

    }
    reverse(ans.begin(),ans.end());
    return ans;
}

int x[2000200]={};

int main()
{

    ios::sync_with_stdio(false);
    cin.tie(0);

    int i,j,a,b,y,z=0,maxi=0,s=0;
    cin>>y;
    for(i=0;i<y;i++)
    {
        cin>>a>>b;
        x[a]=b;
        z=max(z,a);
    }
    i=1;
    while(1)
    {
        if(s==0&&z<i)break;
        if(s>0)s--;
        s+=x[i];
        if(s>maxi)
            maxi=s;
        i++;
    }
    cout<<i-1<<' '<<maxi;

    return 0;
}

double Fib(double x,double z){
    int i=0;
    double f=x,r=x,t;
    for(i=2;i<z;i++)
    {
        t=f;
        f+=r;
        r=t;
    }
    return f;

}
string BinDec( ull x){
    string z;
    while(1)
    {
        z+=((x%3)+'0');
        x/=3;
        if(x==0)
            break;
    }
    while(1)
    {
        if(z.size()==30)
            break;
        z+='0';
    }
    reverse(z.begin(),z.end());
    return z;
}
ull BinDec(string x){
    long long i;
    unsigned long long z=1,a=0;
    for(i=x.size()-1;i>=0;i--)
    {
        if(x[i]=='1')
            a+=z;
        if(x[i]=='2')
            a+=(2*z);
        z*=3;
    }
    return a;
}
ull StringInt(string x){
   ull z=0,i;
   for(i=0;i<x.size();i++)
   {
       z*=2;
       z+=(x[i]-'0');
   }
   return z;
}
string StringInt(ll x){
    string z;
    while(x!=0)
    {
        z+=(x%10+'0');
        x/=10;
    }
    reverse(z.begin(),z.end());
    return z;
}
ull POWMOD (ull base,ull exp,ull mod){
    ull res=1;
    while(exp>0){
        if(exp%2==1)
            res=(res*base);
            res%=mod;
        exp/=2;
        base=(base*base);
        base%=mod;
    }
return res%mod;
}
ull POWE(long long base,long long exp){
    ull res=1;
    while(exp>0){
        if(exp%2==1)
            res=(res*base);
        exp/=2;
        base=(base*base);
    }
return res;

}

/*
int s=0,c[1553][1553]={},z;
char a[1550][1550]={};
void lol(char m,int i,int j,int & s)
{

    if(c[i][j+1]==0&&a[i][j+1]==m)
    {
        s++;
        c[i][j+1]=1;
        lol(m,i,j+1,s);
    }
    if(c[i][j-1]==0&&a[i][j-1]==m)
    {
        s++;
        c[i][j-1]=1;
        lol(m,i,j-1,s);
    }
    if(c[i-1][j]==0&&a[i-1][j]==m)
    {
        s++;
        c[i-1][j]=1;
        lol(m,i-1,j,s);
    }
    if(c[i+1][j]==0&&a[i+1][j]==m)
    {
        s++;
        c[i+1][j]=1;
        lol(m,i+1,j,s);
    }
    if(z!=0)
    {
        z--;
        a[i][j]='X';
    }
    return;
}*/


#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

int main() {
    int n; cin >> n;
    int que = 0, pt = 0, ma = 0;
    while (n--) {
        int t, c; cin >> t >> c;
        que -= min(que, t - pt);
        pt = t;
        chmax(ma, que += c);
    }
    cout << pt + que << " " << ma << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	scanf("%d",&n);
	int pre=0,num=0,ms=0;
	while(n--)
	{
		int t,c;
		scanf("%d %d",&t,&c);
		num-=(t-pre);
		num=max(num,0);
		num+=c;
		ms=max(ms,num);
		pre=t;
	}
	printf("%d %d\n",pre+num,ms);
	return 0;
}


// In The Name Of Allah

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define pp pop_back
#define F first
#define S second
#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef vector<pii> vpii;

const int mod = 1000000007LL;
const int inf = 1e18;
const int maxx = 2e3 + 19;

int n;
int cnt = 0;
int ans = 0;
int mx = -inf;
vpii v;

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    v.pb(mp(0, 0));
    for (int i = 0; i < n; i++) {
        int t, c;
        cin >> t >> c;
        v.pb(mp(t, c));
    }
    for (int i = 1; i <= n; i++) {
        mx = max(cnt, mx);
        cnt -= v[i].F - v[i - 1].F;
        if (cnt < 0)
            cnt = 0;
        mx = max(cnt, mx);
        cnt += v[i].S;
        mx = max(cnt, mx);
        ans = v[i].F;
    }
    cout << ans + cnt << ' ' << mx;
    return 0;
}


// In The Name Of Allah

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define pp pop_back
#define F first
#define S second
#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef vector<pii> vpii;

const int mod = 1000000007LL;
const int inf = 1e18;
const int maxx = 2e3 + 19;

int n;
vector<int> v;
int cnt = 0;
int ans = 0;

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        int t, c;
        cin >> t >> c;
        if (v.size() > 0) {
            v.back()--;
            cnt--;
        }
        if (v.size() > 0 && v.back() == 0)
            v.pp();
        v.pb(c);
        cnt += c;
        ans = t;
    }
    for (auto i : v)
        ans += i;
    cout << ans << ' ' << cnt;
    return 0;
}


// In The Name Of Allah

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define pp pop_back
#define F first
#define S second
#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef vector<pii> vpii;

const int mod = 1000000007LL;
const int inf = 1e18;
const int maxx = 2e3 + 19;

int n;
int cnt = 0;
int ans = 0;
int mx = -inf;
vpii v;

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        int t, c;
        cin >> t >> c;
        v.pb(mp(t, c));
    }
    cnt += v[0].S;
    for (int i = 1; i < n; i++) {
        mx = max(cnt, mx);
        cnt -= v[i].F - v[i - 1].F;
        if (cnt < 0)
            cnt = 0;
        mx = max(cnt, mx);
        cnt += v[i].S;
        mx = max(cnt, mx);
        ans = v[i].F;
    }
    cout << ans + cnt << ' ' << mx;
    return 0;
}


#include <iostream>
using namespace std;

int main(){
    int n;
    cin >> n;
    int size;
    int last;
    cin >> last >> size;
    int ans = size;
    for(int i = 1; i < n; i++){
        int t, c;
        cin >> t >> c;
        int timePassed = t - last;
        ans = max(size, ans);
        size = max(0, size - timePassed);
        size += c;
        last = t;
        ans = max(size, ans);
    }
    cout << last + size << ' ' << ans << endl;
    return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <cassert>
#include <iterator>
#include <complex>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 5e2 + 10;
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int tot = 0;
	int n;
	int ans = 0;
	int pre = 0;
	cin >> n;
	int t, c;
	while (n--)
	{
		cin >> t >> c;
		tot = max(0, tot - (t - pre));
		tot += c;
		pre = t;
		ans = max(ans, tot);
	}
	cout << t + tot << ' ' << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define here cout<<"here\n"
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
using namespace std;

const ll mod = 1e9+7;

const int MAX = 1000005;

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

ll powr(ll x, ll n)
{
    ll ans = 1;
    for(int i=1;i<=n;i++)
        ans=ans*x;
    return ans;
}

ll a[MAX];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int n;
    si(n);
    for(int i=0;i<n;i++)
    {
        int x,y;
        si(x),si(y);
        a[x] = y;
    }
    ll last = 0, maxi = 0;
    for(int i=0;i<MAX;i++)
    {
        if(a[i]>0)
        {
            maxi = max(maxi,a[i]);
            a[i]--;
            a[i+1]+=a[i];
            last = i+1;
        }
    }
    if(a[MAX-1]>0)
    {
        last+=a[MAX-1];
    }
    pf("%lld %lld\n",last,maxi);
    return 0;
}


//
//  main.cpp
//  test
//
//  Created by Максим Бачурин on 04.02.17.
//  Copyright © 2017 Максим Бачурин. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <utility>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <algorithm>
#include <iomanip>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const ll mod = 1e9 + 7;
//const double eps = 1e-9;
const int inf = 1e9;
//const double PI = 3.1415926535897932384626433832795;
const int N = 40005;

int main()
{
    //freopen("output.txt", "r", stdin);
    //freopen("output2.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    int inq = 0, pred = 0, mx = 0, t, c;
    for(int i = 0; i < n; i++)
    {
        cin >> t >> c;
        inq -= min(inq, t - pred);
        inq += c;
        mx = max(mx, inq);
        pred = t;
    }
    cout << t + inq << ' ' << mx << endl;
    return 0;
}


#import<bits/stdc++.h>
// #include<iostream>
// #include<math.h>
// #include<string>
using namespace std;

struct temp{
    long long int start,cont;
};

int main()
{
	long long int n,maxtime,m=INT_MIN,s; cin>>n;
    temp a[n];
    for(int i=0;i<n;i++)
    {
        long long int x,y;
        cin>>x>>y;
        a[i].start=x; a[i].cont=y;

    }
    maxtime=a[0].start+a[0].cont;
    m=a[0].cont; s=m;
    for(long long int i=1;i<n;i++)
    {
        if(maxtime>=a[i].start)
        {
            maxtime+=a[i].cont; s+=a[i].cont;
            s+=(a[i-1].start-a[i].start);
            m=max(m,s);
        }
        else
        {
            maxtime=a[i].start+a[i].cont;
            s=a[i].cont;
            m=max(m,s);
        }
    }

    cout<<maxtime<<" "<<m;
}


#import<bits/stdc++.h>
// #include<iostream>
// #include<math.h>
// #include<string>
using namespace std;

struct temp{
    int start,cont;
};

int main()
{
	long long int n,maxtime,m=INT_MIN,s; cin>>n;
    temp a[n];
    for(int i=0;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        a[i].start=x; a[i].cont=y;
        if(i==0) maxtime=a[i].start+a[i].cont;
        else maxtime+=a[i].cont;
    }
    m=a[0].cont; s=m;
    for(int i=1;i<n;i++)
    {
        s+=a[i].cont;
        s+=(a[i-1].start-a[i].start);
        m=max(m,s);
    }
    cout<<maxtime<<" "<<m;
}


//template by chamow
#include<bits/stdc++.h>
/*-------------------------------------------------------- */
using namespace std;

/*-------------------------------------------------------- */

#define rep(i,val,n) for(ll i=val;i<n;i++)
#define per(j,val,n) for(ll j=val;j>=n;j--)
#define pb push_back
#define pi 3.14157
#define mp make_pair
#define MODULO 1000000007
#define INF 1000000000000000
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL);
#define vecit vector<ll> :: iterator
#define vecpit vector< pair<ll, ll> > :: iterator
#define f first
#define s second

/*-------------------------------------------------------- */

typedef long long ll;
typedef vector<bool> boolean;
typedef vector<ll> vec;
typedef vector< pair<ll, ll> > vecpair;

/*-------------------------------------------------------- */

int main()
{
	fastread;
	ll n;
	cin >> n;
	vecpair p;
	rep(i,0,n)
	{
		ll t, c;
		cin >> t >> c;
		p.pb(mp(t, c));
	}
	ll now = 1;
	ll maximum = 0;
	ll times = 0;
	rep(i,0,n-1)
	{
		now = now > 0 ? --now : now;
		now += p[i].s;
		maximum = max(maximum, now);
		now -= min(now, p[i+1].f - p[i].f - 1);
	}
	times = p[n-1].f;
	now = now > 0 ? --now : now;
	now += p[n-1].s;
	maximum = max(maximum, now);
	times += now;
	cout << times << ' ' << maximum;
	return 0;
}


//template by chamow
#include<bits/stdc++.h>
/*-------------------------------------------------------- */
using namespace std;

/*-------------------------------------------------------- */

#define rep(i,val,n) for(ll i=val;i<n;i++)
#define per(j,val,n) for(ll j=val;j>=n;j--)
#define pb push_back
#define pi 3.14157
#define mp make_pair
#define MODULO 1000000007
#define INF 1000000000000000
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL);
#define vecit vector<ll> :: iterator
#define vecpit vector< pair<ll, ll> > :: iterator
#define f first
#define s second

/*-------------------------------------------------------- */

typedef long long ll;
typedef vector<bool> boolean;
typedef vector<ll> vec;
typedef vector< pair<ll, ll> > vecpair;

/*-------------------------------------------------------- */

int main()
{
	fastread;
	ll n;
	cin >> n;
	vecpair p;
	rep(i,0,n)
	{
		ll t, c;
		cin >> t >> c;
		p.pb(mp(t, c));
	}
	ll now = 1;
	ll maximum = 0;
	ll times = 0;
	rep(i,0,n-1)
	{
		--now;
		now += p[i].s;
		maximum = max(maximum, now);
		now -= min(now, p[i+1].f - p[i].f - 1);
	}
	--now;
	times = p[n-1].f;
	now += p[n-1].s;
	maximum = max(maximum, now);
	times += now;
	cout << times << ' ' << maximum;
	return 0;
}


#define ll long long
#define sf scanf
#define pf printf
#define pb push_back
#define mkp make_pair
#define ff first
#define ss second
#define mod 1000000007
#include<bits/stdc++.h>
using namespace std;
ll modexpo(ll x,ll y){if(y==0)return 1;if(y%2){ll viky=modexpo(x,y/2);
return (((x*viky)%mod)*viky)%mod;}else{ll viky=modexpo(x,y/2);return (viky*viky)%mod;
}}
ll fact[100111];
ll ncr(ll n,ll r){
    ll p = (fact[r]*fact[n-r])%mod;

    return (fact[n]*modexpo(p,mod-2))%mod;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	ll t,i,j,k,l,m,n,p,q,r,s=0,a,b,c,d,ans=0,x,y;
	cin>>n;
	vector<pair<ll,ll> >V(n);
	for(i=0;i<n;i++)
	{
		cin>>a>>b;
		V[i]=mkp(a,b);
	}
	sort(V.begin(),V.end());
	ll now=V[0].ss;
	q=now;
	ans=0;
	ll p1=0;
	if(n==1)
	{
		cout<<V[0].ff+V[0].ss<<" "<<V[0].ss<<endl;
		return 0;
	}
	for(i=1;i<n;i++)
	{
		ans=max(q,ans);
		p=V[i].ff-V[i-1].ff;
		now-=p;
		now=max(0ll,now);
		now+=V[i].ss;
		ans=max(now,ans);
		if(i==n-1)
		{
			p1=now+V[i].ff;
		}
	}
	cout<<p1<<" "<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define pp pop_back
#define F first
#define S second
#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef vector<pii> vpii;

const int mod = 1000000007LL;
const int inf = 1e18;
const int maxx = 2e3 + 19;

int n;
int cnt = 0;
int ans = 0;
int mx = -inf;
vpii v;

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    v.pb(mp(0, 0));
    for (int i = 0; i < n; i++) {
        int t, c;
        cin >> t >> c;
        v.pb(mp(t, c));
    }
    for (int i = 1; i <= n; i++) {
        mx = max(cnt, mx);
        cnt -= v[i].F - v[i - 1].F;
        if (cnt < 0)
            cnt = 0;
        mx = max(cnt, mx);
        cnt += v[i].S;
        mx = max(cnt, mx);
        ans = v[i].F;
    }
    cout << ans + cnt << ' ' << mx;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define cheetah ios_base::sync_with_stdio(NULL);cin.tie(0);cout.tie(0)
#define deb1(x) cout<<"["#x<<":"<<x<<"]"<<"  ";
#define deb2(x,y) cout<<"["#x<<":"<<x<<" "#y<<":"<<y<<"]";
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define endl ("\n")
typedef tree<ll int ,null_type,less<ll int> ,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//  *(set_name.find_by_order(k))    set_name.order_of_key(k)

int main (){
	clock_t t1, t2;
    t1 = clock();
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	freopen("error.txt","w",stderr);
	#endif

    cheetah;
    cout<<fixed;
    cout.precision(10);
    int t = 1;
    // cin>>t;
    while(t--){
    	int n;cin>>n;
    	vector<pair<int,int> > v;
    	for(int i = 0;i<n;i++){
    		int x,y;cin>>x>>y;
    		v.pb({x,y});
    	}
    	sort(all(v));
    	int size = 0,time = 0;
    	size += v[0].ss;
    	int ans = size;
    	time += size+v[0].ff;
    	for(int i = 1;i<n;i++){
    		int temp = max(v[i].ff-time,0);
    		time += temp+v[i].ss;
    		size = size - (v[i].ff-v[i-1].ff);
    		if(v[i].ff>time)
    			size = 0;
    		size += v[i].ss;
    		ans = max(ans,size);
    	}
    	cout<<time<<" "<<ans<<endl;

    }

    t2 = clock();
    #ifndef ONLINE_JUDGE
	cerr << "time taken : " << (t2 - t1) / (long double)CLOCKS_PER_SEC<<" seconds";
    #endif
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define cheetah ios_base::sync_with_stdio(NULL);cin.tie(0);cout.tie(0)
#define deb1(x) cout<<"["#x<<":"<<x<<"]"<<"  ";
#define deb2(x,y) cout<<"["#x<<":"<<x<<" "#y<<":"<<y<<"]";
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define endl ("\n")
typedef tree<ll int ,null_type,less<ll int> ,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//  *(set_name.find_by_order(k))    set_name.order_of_key(k)

int main (){
	clock_t t1, t2;
    t1 = clock();
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	freopen("error.txt","w",stderr);
	#endif

    cheetah;
    cout<<fixed;
    cout.precision(10);
    int t = 1;
    // cin>>t;
    while(t--){
    	int n;cin>>n;
    	vector<pair<int,int> > v;
    	for(int i = 0;i<n;i++){
    		int x,y;cin>>x>>y;
    		v.pb({x,y});
    	}
    	sort(all(v));
    	int size = 0,time = 0;
    	size += v[0].ss;
    	time += size+v[0].ff;
    	for(int i = 1;i<n;i++){
    		int temp = max(v[i].ff-time,0);
    		time += temp+v[i].ss;
    		size = size - (v[i].ff-v[i-1].ff);
    		size += v[i].ss;
    	}
    	cout<<time<<" "<<size<<endl;

    }

    t2 = clock();
    #ifndef ONLINE_JUDGE
	cerr << "time taken : " << (t2 - t1) / (long double)CLOCKS_PER_SEC<<" seconds";
    #endif
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define cheetah ios_base::sync_with_stdio(NULL);cin.tie(0);cout.tie(0)
#define deb1(x) cout<<"["#x<<":"<<x<<"]"<<"  ";
#define deb2(x,y) cout<<"["#x<<":"<<x<<" "#y<<":"<<y<<"]";
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define endl ("\n")
typedef tree<ll int ,null_type,less<ll int> ,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//  *(set_name.find_by_order(k))    set_name.order_of_key(k)

int main (){
	clock_t t1, t2;
    t1 = clock();
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	freopen("error.txt","w",stderr);
	#endif

    cheetah;
    cout<<fixed;
    cout.precision(10);
    int t = 1;
    // cin>>t;
    while(t--){
    	int n ;
	    cin >> n;

	    int start = 0,sum=0,ma=0;
	    for(int i = 0 ;i <n;i++)
	    {
	        int x,y;
	        cin >> x >>y;
	        if(start<x)
	            sum=0;
	        else
	            sum=start-x;
	        sum+=y;
	        ma=max(ma,sum);
	        start=max(x,start)+y;
	    }
	    cout << start<< " " << ma<<endl;

    }

    t2 = clock();
    #ifndef ONLINE_JUDGE
	cerr << "time taken : " << (t2 - t1) / (long double)CLOCKS_PER_SEC<<" seconds";
    #endif
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define cheetah ios_base::sync_with_stdio(NULL);cin.tie(0);cout.tie(0)
#define deb1(x) cout<<"["#x<<":"<<x<<"]"<<"  ";
#define deb2(x,y) cout<<"["#x<<":"<<x<<" "#y<<":"<<y<<"]";
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define endl ("\n")
typedef tree<ll int ,null_type,less<ll int> ,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//  *(set_name.find_by_order(k))    set_name.order_of_key(k)

int main (){
	clock_t t1, t2;
    t1 = clock();
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	freopen("error.txt","w",stderr);
	#endif

    cheetah;
    cout<<fixed;
    cout.precision(10);
    int t = 1;
    // cin>>t;

    while(t--){
	    	int n,m1;cin>>n>>m1;
	    	map<int,int> m;
	    	for(int i = 0;i<m1;i++){
	    		int x,y;cin>>x>>y;
	    		m[x]++;
	    		m[y]++;
	    	}
	    	int count = 0;
	    	for(int i = 1;i<=n;i++){
	    		if(m[i]==m1){
	    			cout<<"star topology"<<endl;
	    			return 0;
	    		}
	    		if(m[i]>2){
	    			cout<<"unknown topology"<<endl;
	    			return 0;
	    		}
	    		if(m[i]==2){
	    			count++;
	    		}
	    	}
	    	if(count==n){
	    		cout<<"ring topology"<<endl;
	    		return 0;
	    	}
	    	else if(count==n-2){
	    		cout<<"bus topology"<<endl;
	    		return 0;
	    	}
	    	cout<<"unknown topology"<<endl;

    }

    t2 = clock();
    #ifndef ONLINE_JUDGE
	cerr << "time taken : " << (t2 - t1) / (long double)CLOCKS_PER_SEC<<" seconds";
    #endif
  return 0;
}


#pragma comment(linker,"/STACK:64000000")
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <stack>
#include <functional>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf32 = 1e9 + 9;
const ll inf64 = 1e18 + 18;
const int N = 1e5 + 5;
const ll mod = 1e9 + 7;

void solve()
{
	ll n, t, c;
	cin >> n >> t >> c;
	ll mx = c, curt = t, curc = c;
	for (int i = 1; i < n; ++i)
	{
		cin >> t >> c;
		curc = max(0LL, curc - t + curt) + c;
		curt = t;
		mx = max(mx, curc);
	}
	cout << curt + curc << " " << mx;
}

int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif
	solve();
#ifdef _DEBUG
	cout << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


#include<iostream>
using namespace std;

int main()
{
    register int maxn,n,t,x
    cin>>n;
    for(register int i=1;i<=n;i++)
    {
    register    int in1,in2;
        cin>>in1>>in2;
        x-=in1-t;
        if(x<0)
            x=0;
        x+=in2;
        maxn=max(maxn,x);
        t=in1;
    }
    cout<<t+x<<" "<<maxn;
    return 0;
}


#include <cstdio>
#include <iostream>

int time, maxsize, n, tmp;

int main()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
	{
		int T, C;
		scanf("%d %d", &T, &C);
		tmp -= T - time;
		if(tmp < 0)
			tmp = 0;
		tmp += C;
		maxsize = std::max(tmp, maxsize);
		time = T;
	}
	printf("%d %d\n", time + tmp, maxsize);
	return 0;
}


#include<iostream>
using namespace std;

int main()
{
    register int maxn,n,t,x;
    cin>>n;
    for(register int i=1;i<=n;i++)
    {
    register    int in1,in2;
        cin>>in1>>in2;
        x-=in1-t;
        if(x<0)
            x=0;
        x+=in2;
        maxn=max(maxn,x);
        t=in1;
    }
    cout<<t+x<<" "<<maxn;
    return 0;
}


#include <cstdio>
#include <queue>
#include <iostream>
using namespace std;

int n, *T, *C, maxsize;
deque<int> Q;

int main()
{
	scanf("%d", &n);
	T = new int[n + 1];
	C = new int[n + 1];
	for(int i = 1; i <= n; i++)
	{
		scanf(" %d %d", &T[i], &C[i]);
		for(int j = 1; j <= T[i] - T[i - 1] && !Q.empty(); j++)
			Q.pop_front();
		for(int j = 1; j <= C[i]; j++)
			Q.push_back(j);
		if(Q.size() > maxsize)
			maxsize = Q.size();
	}
	printf("%d %d\n", T[n] + Q.size(), maxsize);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ll t;
	cin>>t;
	ll a[t],b[t],sum=0,i,maxx;
	for(i=0;i<t;i++)
	{
		cin>>a[i]>>b[i];
	}
	sum+=b[0];
	maxx=sum;
	for(i=1;i<t;i++)
	{
		if(maxx<sum)
			maxx=sum;
		if((a[i]-a[i-1])<=sum+b[i])
		sum+=b[i]-(a[i]-a[i-1]);
	    else
		sum=b[i];

	}
	if(sum>maxx)
		maxx=sum;
	cout<<sum+a[t-1]<<" "<<maxx;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ll t;
	cin>>t;
	ll a[t],b[t],sum=0,i,maxx=INT_MIN;
	for(i=0;i<t;i++)
	{
		cin>>a[i]>>b[i];
	}
	sum+=b[0];
	for(i=1;i<t;i++)
	{
		if(maxx<sum)
			maxx=sum;
		if((a[i]-a[i-1])<=sum+b[i])
		sum+=b[i]-(a[i]-a[i-1]);
	    else
		sum=b[i];

	}

	cout<<sum+a[t-1]<<" "<<maxx;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ll t;
	cin>>t;
	ll a[t],b[t],sum=0,i;
	for(i=0;i<t;i++)
	{
		cin>>a[i]>>b[i];
	}
	sum+=b[0];
	for(i=1;i<t;i++)
	{
		sum+=b[i]-(a[i]-a[i-1]);
	}

	cout<<sum+a[t-1]<<" "<<sum;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ll t;
	cin>>t;
	ll a[t],b[t],sum=0,i,maxx;
	for(i=0;i<t;i++)
	{
		cin>>a[i]>>b[i];
	}
	sum+=b[0];
	maxx=sum;
	for(i=1;i<t;i++)
	{

		if((a[i]-a[i-1])<=sum)
		sum=sum-(a[i]-a[i-1]);
	    else
		sum=0;
	sum+=b[i];
	if(maxx<sum)
			maxx=sum;

	}

	cout<<sum+a[t-1]<<" "<<maxx;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long n, t, c, a, b;

int main() {
    ios::sync_with_stdio(0);
#ifdef home
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    cin >> n;
    while (n--) {
        cin >> t >> c;
        if (a < t) {
            a = t + c;
            b = max(b, c);
        }
        else {
            b = max(b, a - t + c);
            a = a + c;
        }
    }
    cout << a << ' ' << b;
}


#include<iostream>
using namespace std;
int main()
{
	int n, t, c, i, j, q = 0;
	cin >> n;
	int Q[n], T[n];
	for (i = 0; i < n; i++)
		cin >> T[i] >> Q[i];
	q = Q[0];
	j = q;
	t = T[0];
	for (i = 1; i <n ; i++)
	{
		q += Q[i] - (T[i] - T[i - 1]);
		if (q > j)
			j = q;
		t += T[i];
	}
	cout << t << " " << j;

}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+7;
int n,w[maxn],maxw,task,t[maxn],c[maxn];

int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>t[i]>>c[i];
		if(t[i]>=task){task=t[i]+c[i];w[i]=c[i];}
		else{task+=c[i];w[i]=w[i-1]+c[i]+t[i-1]-t[i];}
		maxw=max(w[i],maxw);
	}
	cout<<task<<" "<<maxw;
}


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mod 1000000007
#define mod1 998244353
using namespace std;
ll modi(ll a,ll p) {
	ll res=1;
	while(p) {
		if(p&1) res=res*a%mod1;
		a=a*a%mod1;
		p>>=1;
	}
	return res;
}

int main()
{
    ll n;
    cin>>n;
    ll curr=0,tt=0,m=0;
    while(n--)
    {
        ll t,c;
        cin>>t>>c;

        curr-=(t-tt);
        if(curr<0)
            curr=0;
        curr+=c;
        if(curr>m)
            m=curr;
        tt=t;
    }
    cout<<tt+curr<<" "<<m<<endl;

    }


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mod 1000000007
#define mod1 998244353
using namespace std;
ll modi(ll a,ll p) {
	ll res=1;
	while(p) {
		if(p&1) res=res*a%mod1;
		a=a*a%mod1;
		p>>=1;
	}
	return res;
}

int main()
{
    ll n;
    cin>>n;
    ll curr=0,tt=0,m=0;
    while(n--)
    {
        ll t,c;
        cin>>t>>c;

        curr-=(t-tt);
        if(curr<0)
            curr=0;
        curr+=c;
        if(curr>m)
            m=curr;
        tt=t;
    }
    cout<<tt+curr<<" "<<endl;

    }


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
#define pb push_back
#define all(v) v.begin(),v.end()
#define EPS 1e-9
#define MOD 1000000007
#define MAX 100005
typedef pair<int,int> pii;
typedef long long ll;

int t[MAX];
int c[MAX];

int main()
{
    int n;
    int q = 0;
    int s = 0;

    scanf("%d",&n);

    int qq = 0;

    for(int i=1; i<=n; i++)
    {
        scanf("%d %d",t + i,c + i);

        if(i > 1)
        {
            int d = t[i] - t[i - 1];
            if(q >= d) q -= d;
            else q = 0;
        }

        q += c[i];
        qq = max(qq,q);
    }

    s = t[n] + q - c[n] + c[n];

    printf("%d %d",s,qq);

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const long long int mod=1e9 + 7;
#define sf scanf
#define pf printf

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

int main() {

  ll n,m,i,j,t,k,q;
  ll maxa=-1;
  ll tim=0;
  sf("%lld",&n);
  queue<ll> qq;
 pair<ll,ll> p[n];
 for(i=0;i<n;i++)
  {
      sf("%lld%lld",&t,&k);
      p[i]=(make_pair(t,k));

  }
  sort(p,p+n);
  ll ind=0;
  ll siz=0;
  for(i=1;i<=1000000;i++)
  {
      if(siz>0)
      {
          maxa=max(maxa,siz);
          siz--;
          tim=max(tim,i);
      }
      if(p[ind].first==i)
      {
          ll sec=p[ind].second;
          siz+=sec;
          ind++;

      }

  }
  if(siz>0)
  {tim=siz+1e6;maxa=max(siz,maxa);}

  cout<<tim<<" "<<maxa;

  return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;

#define MAX 300050

int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int n;
    cin>>n;
    ll qq=0,prev,ans=0,zeros=0;

    for(int i=0;i<n;i++){
        ll t,x;
        cin>>t>>x;

        if(i!=0){
            qq-=(t-prev);
        }

        qq=max(qq,zeros);
        qq+=x;
        prev=t;
        ans=max(ans,qq);
    }

    cout<<prev+qq<<" "<<ans<<endl;
    return 0;

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;

#define MAX 300050

int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int n;
    cin>>n;
    int qq=0,prev,ans=0;

    for(int i=0;i<n;i++){
        int t,x;
        cin>>t>>x;
        qq+=x;
        if(i!=0){
            qq-=(t-prev);
        }

        prev=t;
        ans=max(ans,qq);
    }

    cout<<prev+qq<<" "<<ans<<endl;
    return 0;

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;

#define MAX 300050

int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int n;
    cin>>n;
    ll qq=0,prev,ans=0;

    for(int i=0;i<n;i++){
        ll t,x;
        cin>>t>>x;
        qq+=x;
        if(i!=0){
            qq-=(t-prev);
        }

        prev=t;
        ans=max(ans,qq);
    }

    cout<<prev+qq<<" "<<ans<<endl;
    return 0;

}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;

    cin >> n;
    int time = 0;
    int size_ = 0, maxSize = 0;
    for(int i = 1; i <= n; i++) {
        int u, v;
        cin >> u >> v;
        if (time < u) {
            if (size_ > 0) {
                size_ -= min(u - time, size_);
            }
            time = u;
        }
        size_ += v;
        maxSize = max(maxSize, size_);
    }
    cout << time + size_ << " " << maxSize << endl;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <ctime>
#include <vector>
#include <set>
#include <iterator>
#include <map>

#define ll long long
#define ld long double

#define y1 abc

#define endl '\n'

#define fi first
#define se second
#define m_p make_pair

#define pb push_back
#define pob pop_back

#define MAXLL 9000000000000000000LL
#define MAXINT 2000000000

#define MINLL -9000000000000000000LL
#define MININT -2000000000

using namespace std;

// <constants>

const ld pi = acos(-1);

// </constants>

int n, i, j, mx, c;

pair < int, int > arr[1501];

int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;

    for (i = 1; i <= n; i++){
        cin >> arr[i].fi >> arr[i].se;
    }

    c = arr[1].se;

    mx = arr[1].se;

    for (i = 2; i <= n; i++){
        c -= min(arr[i].fi - arr[i - 1].fi, c);

        c += arr[i].se;

        mx = max(c, mx);
    }

    cout << arr[n].fi + c << " " << mx << endl;

    return 0;
}


#import<iostream>
using namespace std;int i,j,m,n,q,w,c[1005],t[1005];main(){for(cin>>n;j<n;j++)cin>>t[j]>>c[j];for(;i<n||q;w<t[i]|i==n?:m=(q+=c[i++])>m?q:m)w++,q-=!!q;cout<<w<<' '<<m;}


#import<iostream>
using namespace std;int i,j,m,n,q,w,c[1005],t[1005];main(){for(cin>>n;j<n;j++)cin>>t[j]>>c[j];for(;i<n;w<t[i]|i==n?:m=(q+=c[i++])>m?q:m)w++,q-=!!q;cout<<w+q<<' '<<m;}


#import<iostream>
using namespace std;int i,j,m,n,q,w,c[1005],t[1005];main(){for(cin>>n;j<n;j++)cin>>t[j]>>c[j];for(;i<n||q;w<t[i]&i<n?:m=(q+=c[i++])>m?q:m)w++,q-=!!q;cout<<w<<' '<<m;}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n,m) for(int i=n;i<m;i++)
#define ll long long
const int maxn=4444444;
int ss[maxn],ff[maxn],deg[maxn];
string res="unknown topology";
int main()
{
    int n;
    cin>>n;
    int cur=0,ptr=0,id=0;
    rep(i,0,n)
    {
        int l,r;
        cin>>l>>r;
        cur-=(l-ptr);
        if(cur<0)
        cur=0;
        cur+=r;
        if(cur>id)
        id=cur;
        ptr=l;
    }
    cout<<ptr+cur<<' '<<id;
    return 0;
}
//remember overflow and check the corner case


#include "bits/stdc++.h"

using namespace std;
using LL = long long;

constexpr int N = 1e5 + 5;

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int n;
	cin >> n;

	int cur_size = 0, max_size = 0;

	deque< pair< int, int>> Q;
	int T = 0;

	for (int i = 0; i < n; i++) {
		int t, c;
		cin >> t >> c;

		while (!Q.empty() && Q.front().first <= t) {
			auto p = Q.front(); Q.pop_front();

			if (p.second <= t) {
				cur_size -= p.second - p.first + 1;
			}
			else {
				cur_size -= t - p.first + 1;
				Q.push_front({t + 1, p.second});
			}
		}

		T = max(t, T);
		if (!Q.empty()) T = max(t, Q.back().second);

		Q.push_back({T + 1, T + c});
		cur_size += c;
		max_size = max(max_size, cur_size);
	}

	cout << Q.back().second << ' ' << max_size << "\n";
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <set>
#include <list>
#include <map>
#include <queue>
#include <iterator>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <utility>
#include <functional>
#include <stack>
#include <deque>
#include <sstream>
using namespace std;

#define ll long long
#define mod 1000000007
#define ld long double
#define infinity (ll)1e18+1
#define PI 3.14159265358979

#define pdd pair<ld,ld>
#define pll pair<ll, ll>
#define pii pair<int,int>

#define MP make_pair
#define SZ size()
#define PB push_back

#define vi vector<int>//ll
#define vll vector<ll>
#define vpll vector<pll>
#define vpii vector<pii>
#define vch vector<char>
#define vb vector<bool>
#define vld vector<ld>
#define vs vector<string>

#define FOR(i,a,b) for(ll i = (a); i < (b); ++i)
#define RFOR(i,b,a) for(ll i = (b) - 1; i >= (a); --i)
#define REPEAT(i) FOR(counter1234,0,i)
#define ALL(a) a.begin(), a.end()
#define X first
#define Y second
#define MAXN 101
clock_t startTime;

int main()
{
	srand(time(NULL));
	startTime = clock();
	ios_base::sync_with_stdio(false), cin.tie(0);
	//ifstream in("In.txt");

	int n;
	cin >> n;

	vi t(n), c(n);
	FOR(i, 0, n)
		cin >> t[i] >> c[i];

	ll curr = 0;
	FOR(i, 0, n)
	{
		if (curr <= t[i])
			curr = t[i];
		curr += c[i];
	}

	ll m = c[0];
	ll ye = c[0];
	int last = 0;
	FOR(i, t[0] + 1, t[n - 1] + 1)
	{
		if (ye > 0)
			--ye;

		if (t[last + 1] == i)
		{
			last++;
			ye += c[last];
		}

		m = max(m, ye);
	}

	cout << curr << " " << m;
	return 0;
}
//don't forget about MAXN, mod and infinity


#include <iostream>
#include <string>
#include <math.h>       /* pow */
#include <vector>
#include <queue>          // std::queue
#include <algorithm>

using namespace std;
class SMS {
   public:
      int time;
      int work;
};

int main() {
  int n;
  cin >> n;
/*
3
3 3
4 3
5 3
*/
  SMS sms;
  sms.time = 0;
  sms.work = 0;
  int queueSize = 0;
  int lastWorkTime = 0;
  for (int i = 0; i < n; ++i) {
    int t, c;
    cin >> t >> c;
    if (sms.time == 0 && sms.work == 0) {
      sms.time = t;
      sms.work = c;
      queueSize = max(queueSize, c);
    } else {
      sms.work = (c + max(sms.work - (t - sms.time), 0));
      sms.time = t;
      queueSize = max(queueSize, sms.work);
    }
  }
  lastWorkTime = sms.time + sms.work;
  cout << lastWorkTime << " " << queueSize << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int mx = -1, n,prev,q,t,m;

int main () {
	cin >> n;
	cin >> prev >> q;
	mx = q;
	for (int i = 2 ; i <= n ; i++) {
		cin >> t >> m;
		mx = max (q,mx);
		q -= (t-prev);
		q = max(q,0);
		q += m;
		mx = max (q,mx);
		prev = t;
	}
	cout << prev+q << " " << mx << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int mx = -1, n,prv,q,t,m;

int main () {
	cin >> n;
	cin >> prv >> q;
	mx = q;
	for (int i = 2 ; i <= n ; i++) {
		cin >> t >> m;
		mx = max (q,mx);
		q -= (t-prv);
		q = max(q,0);
		q += m;
		mx = max (q,mx);
		prv = t;
	}
	cout << prv+q << " " << mx << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int c,t;
    pair<int,int> ar[n];
    pair<long long,long long> ans[n];

    for(int i=0;i<n;i++){
    cin>>ar[i].first>>ar[i].second;
    }
    sort(ar,ar+n);
    long long nn = ar[0].first+ar[0].second;
    ans[0] = make_pair(ar[0].first,nn);
    for(int i=1;i<n;i++){
        if(ar[i].first<=nn)
            nn+=ar[i].second;
        else
            nn = ar[i].second+ar[i].first;

    ans[i] = make_pair(ar[i].first,nn);
    }
    long long xx = ar[0].second;
    for(int i=1;i<n;i++){

        xx = max(xx,ans[i-1].second - ans[i].first+ar[i].second);
    }

    cout<<nn<<" "<<xx;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    pair<long long,long long> ar[n];
    pair<long long,long long> ans[n];

    for(int i=0;i<n;i++){
    cin>>ar[i].first>>ar[i].second;
    }
    sort(ar,ar+n);
    long long nn = ar[0].first+ar[0].second;
    ans[0] = make_pair(ar[0].first,nn);
    for(int i=1;i<n;i++){
        if(ar[i].first<=nn)
            nn+=ar[i].second;
        else
            nn = ar[i].second+ar[i].first;

    ans[i] = make_pair(ar[i].first,nn);
    }
    long long xx = ar[0].second;
    for(int i=1;i<n;i++){

        xx = max(xx,ans[i-1].second - ans[i].first+ar[i].second);
    }

    cout<<nn<<" "<<xx;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    pair<long long,long long> ar[n];
    pair<long long,long long> ans[n];

    for(int i=0;i<n;i++){
    cin>>ar[i].first>>ar[i].second;
    }
    sort(ar,ar+n);
    long long nn = ar[0].first+ar[0].second;
    ans[0] = make_pair(ar[0].first,nn);
    for(int i=1;i<n;i++){
        if(ar[i].first<=nn)
            nn+=ar[i].second;
        else
            nn = ar[i].second+ar[i].first;

    ans[i] = make_pair(ar[i].first,nn);
    }
    long long xx = ar[0].second;
    for(int i=1;i<n;i++){
        if(ans[i-1].second<ans[i].first){
            xx = max(xx,ar[i].second);
        }
        else
        xx = max(xx,ans[i-1].second - ans[i].first+ar[i].second);
    }

    cout<<nn<<" "<<xx;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long  i, j, n, c[123456],a[1111234], b[1125678],d[123456], l,ll,r, s, t, k, m,x, y,y1,z;
string p,q;
map <long long,long long> mm;
int main() {
    cin >> n;
    for (i = 0; i < n; i ++){
        cin >> a[i]>>b[i];
        t = max (0ll,t -a[i]+a[i-1]);
        t += b[i];
        x = max (t,x);
        s =max (a[i] ,s);
        s += b[i];
    }
    cout << s <<' '<<x;
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
long long int a[1000007];
long long int b[1000007];
int main()
{
	int m;
	scanf("%d",&m);
	int i;
	for(i=0;i<m;i++)
		scanf("%lld %lld",a+i,b+i);
	long long int anss=0;
	long long int ansss=0;
	long long int ans=0;
	for(i=0;i<m;i++)
	{
		anss=anss+b[i];
		ansss=max(ansss,anss);
		ans=max(ans,a[i]+anss);
		if(i<m)
			anss=anss-min(anss,a[i+1]-a[i]);
	}
	printf("%lld %lld\n",ans,ansss);
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <iomanip>
#include <utility>
#include <stack>
#include <memory.h>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <inttypes.h>

#define FILE ""
#define fi first
#define se second
#define pb push_back
#define matrix(type) vector<vector<type>>
#define sqr(x) ((x)*(x))
using namespace std;

typedef long long LL;
typedef long double LD;

const int INF = 2e9;
const LD PI = 3.14159265358979323846;
const LD EPS = 1e-10;
const int MOD = 1e9 + 7;

const int N = 1e3 + 10;
const int M = 1e5 + 10;

using namespace std;

int main(){
	//freopen(FILE".in", "r", stdin); freopen(FILE".out", "w", stdout);
	int n, t, cnt = 0, mx = 0, c, dt, tp = 0;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> t >> c;
		dt = t - tp;
		cnt -= min(dt, cnt);
		tp = t;
		cnt += c;
		mx = max(mx, cnt);
	}
	cout << t + cnt << " " << mx << endl;
	return 0;
}


#include <bits/stdc++.h>
#define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout)
#define STREAMFAST ios::sync_with_stdio(0)
using namespace std;

int main()
{
    //FILES;
    STREAMFAST;
    int n; cin >> n;
    int size, last_time, m_size; cin >> last_time >> size;
    m_size = size;
    for (int i = 1; i < n; i++)
    {
        int t, c; cin >> t >> c;
        size -= (t - last_time > size ? size : t - last_time) - c;
        last_time = t;
        m_size = max(m_size, max(c, size));
    }
    cout << last_time + size << ' ' << m_size;
    return 0;
}


#include <bits/stdc++.h>
#define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout)
#define STREAMFAST ios::sync_with_stdio(0)
using namespace std;

int main()
{
    //FILES;
    STREAMFAST;
    int n; cin >> n;
    int size, last_time; cin >> last_time >> size;
    for (int i = 1; i < n; i++)
    {
        int t, c; cin >> t >> c;
        size -= abs(last_time - t) - c;
        last_time = t;
    }
    cout << last_time + size << ' ' << size;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
#define ITER(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MAX = 1000 + 47;
int T[MAX];
int C[MAX];

int main()
{
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false); cin.tie(0);
	int n;
	cin >> n;
	FOR(i, 0, n)
	{
		cin >> T[i] >> C[i];
	}

	int cnt = 0;
	int mx = 0;
	FOR(i, 0, n)
	{
		if (i)
		{
			cnt -= T[i] - T[i - 1];
			cnt = max(cnt, 0);
		}

		cnt += C[i];
		mx = max(mx, cnt);
	}

	int t = T[n - 1] + cnt;
	cout << t << " " << mx << "\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a,b,c,d=-1,e,f,g;
int main()
{
	cin>>a;
	while(++d<a)
	{
		cin>>b>>c;
		e=max(e-b+g+c,c);
		if(e>f)
		{
			f=e;
		}
		g=b;
	}
	cout<<b+e<<" "<<f;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {

     long long n,h,i,j,t,m,l;
     long long p=0,s,k,w,z,x,y;
     std::cin >> n;
     int a[n];int b[n];
     for(i=0;i<n;i++)
          std::cin >> a[i] >> b[i];
     x=b[0];
     m=b[0];
     for(i=1;i<n;i++)
     {
          x=x-(a[i]-a[i-1]);
          x=x+b[i];
          if(x>m)
               m=x;
     }
     t=x+a[n-1];
     std::cout << t << " " << m << std::endl;
     return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {

     long long n,h,i,j,t,m,l;
     long long p=0,s,k,w,z,x,y;
     std::cin >> n;
     int a[n];int b[n];
     for(i=0;i<n;i++)
          std::cin >> a[i] >> b[i];
     x=b[0];
     m=b[0];
     for(i=1;i<n;i++)
     {
          x=x-min(x,(a[i]-a[i-1]));
          x=x+b[i];
          if(x>m)
               m=x;
     }
     t=x+a[n-1];
     std::cout << t << " " << m << std::endl;
     return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {

     long long n,h,i,j,t,m,l;
     int p=0,s,k,w,z,x,y;
     std::cin >> n;
     int a[n];int b[n];
     for(i=0;i<n;i++)
          std::cin >> a[i] >> b[i];
     x=b[0];
     m=b[0];
     for(i=1;i<n;i++)
     {
          x=x-min(x,(a[i]-a[i-1]));
          x=x+b[i];
          if(x>m)
               m=x;
     }
     t=x+a[n-1];
     std::cout << t << " " << m << std::endl;
     return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN =1e6 + 11;
int n;
int t[MAXN],m[MAXN];

int main(){
    scanf("%d",&n);
    int totalmessages = 0;
    int maxmsg = 0;
    int lastmessage = -1;
    t[0] = 0;
    m[0] = 0;
    int lasttime = -1;

    for(int i = 1;i<=n;i++){
        scanf("%d %d",t + i,m + i);
        if(totalmessages == 0 || (t[i]-t[i-1]) >= totalmessages){
            totalmessages = m[i];
        }
        else{
            totalmessages = totalmessages-(t[i] - t[i-1]) + m[i];
        }
        maxmsg = max(totalmessages,maxmsg);
    }
    printf("%d %d\n",totalmessages + t[n],maxmsg);
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin>>n;

    int prev = -1,s = 0,lt = 0,ans=-1;
    for(int i=0;i<n;i++)
    {
        int t,m;
        cin>>t>>m;

        if(t-prev >= s) s=m;
        else s=s+m-(t-prev);

        if(i==n-1) lt=t;

        //cout<<prev<<" "<<s<<endl;
        ans = max(ans,s);

        prev = t;
    }
    cout<<lt+s<<" "<<ans;
}


#include <iostream>
#include <queue>
using namespace std;

int main()
{
    int n;
    cin>>n;

    //queue <int> q;
    int lt=0;
    int d=0,p=0,s=0;
    for(int i=0;i<n;i++)
    {
        int t,m;
        cin>>t>>m;

        if(i!=0) d=t-p;
        else d=0;

        s=s+m-d;
        if(i==n-1) lt=t;
        p=t;
    }
    cout<<lt+s<<" "<<s;
}


#include <iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;

    int prev = -1,s = 0,lt = 0;
    for(int i=0;i<n;i++)
    {
        int t,m;
        cin>>t>>m;

        if(t-prev >= s) s=m;
        else s=s+m-(t-prev);

        if(i==n-1) lt=t;

        prev = t;
    }
    cout<<lt+s<<" "<<s;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 1e3 + 10;
int t[N], c[N];

int main() {
    ll n;
    cin >> n;
    for (ll i = 0; i < n; i++)
        cin >> t[i] >> c[i];
    ll ans = c[0], sum = c[0];
    for (ll i = 1; i < n; i++) {
        sum = max((ll)0, sum - (t[i] - t[i - 1]));
        sum += c[i];
        ans = max(ans, sum);
    }
    cout << sum + t[n - 1] << " " << ans << endl;
    return 0;
}


// https://serhatgiydiren.github.io/

#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(char c) {
  return to_string(string(1,c));
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define D(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define D(...) 404
#endif

//freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);

void solve(const int &test_id)
{
 int n,a,b,c=0,q=0;
 cin >> n;
 while(n--)
 {
  cin >> a >> b;
  if (c) c--;
  c+=b;
  q=max(q,c);
 }
 cout << a+c << " " << c << endl;
}

void solve_cases()
{
 int test_cases=1;
 //cin >> test_cases;
 for(int i=1;i<=test_cases;i++) solve(i);
}

void fast_io()
{
 ios::sync_with_stdio(false);
 srand(time(NULL));
 cin.tie(0);
 cout.tie(0);
 cout << fixed << setprecision(15);
 cerr << fixed << setprecision(15);
}

int main()
{
 fast_io();
 solve_cases();
 return EXIT_SUCCESS;
}


// https://serhatgiydiren.github.io/

#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(char c) {
  return to_string(string(1,c));
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define D(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define D(...) 404
#endif

//freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);

void solve(const int &test_id)
{
 int n,a,b,c=0,p=0,q=0;
 cin >> n;
 while(n--)
 {
  cin >> a >> b;
  c=max(0,c-(a-p));
  c+=b;
  q=max(q,c);
  p=a;
 }
 cout << p+c << " " << q << endl;
}

void solve_cases()
{
 int test_cases=1;
 //cin >> test_cases;
 for(int i=1;i<=test_cases;i++) solve(i);
}

void fast_io()
{
 ios::sync_with_stdio(false);
 srand(time(NULL));
 cin.tie(0);
 cout.tie(0);
 cout << fixed << setprecision(15);
 cerr << fixed << setprecision(15);
}

int main()
{
 fast_io();
 solve_cases();
 return EXIT_SUCCESS;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main(){
   ll n, first, last, sum, maximum;
   cin >> n;
   ll a[10000000] = {0};
   for(ll i = 0; i < n; ++i){
       ll t, c;
       cin >> t >> c;
       a[t] = c;
       if(i == 0){
           first = t;
           sum = c;
           maximum = c;
       }
       if(i == (n - 1)){
           last = t;
       }
   }
   for(ll i = first + 1; i <= 10000000; ++i){
       if(sum > 0)
       sum --;
       sum += a[i];
       maximum = max(sum, maximum);
       if(sum == 0 && i >= last){
           cout << i << " " << maximum;
           break;
       }
   }

}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main(){
   ll n, first, last, sum, maximum;
   cin >> n;
   ll a[10000001] = {0};
   for(ll i = 0; i < n; ++i){
       ll t, c;
       cin >> t >> c;
       a[t] = c;
       if(i == 0){
           first = t;
           sum = c;
           maximum = c;
       }
       if(i == (n - 1)){
           last = t;
       }
   }
   for(ll i = first + 1; i <= 10000000; ++i){
       if(sum > 0)
       sum --;
       sum += a[i];
       maximum = max(sum, maximum);
       if(sum == 0 && i >= last){
           cout << i << " " << maximum;
           break;
       }
   }

}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main(){
   ll n, first, last, sum, maximum;
   cin >> n;
   ll a[10000001] = {0};
   for(ll i = 0; i < n; ++i){
       ll t, c;
       cin >> t >> c;
       a[t] = c;
       if(i == 0){
           first = t;
           sum = c;
           maximum = c;
       }
       if(i == (n - 1)){
           last = t;
       }
   }
   for(ll i = first + 1; i <= 10000000; ++i){
       if(sum > 0)
       sum --;
       sum += a[i];
       maximum = max(sum, maximum);
       if(sum == 0 && i >= last){
           cout << i % 1000000000 << " " << maximum % 1000000000;
           break;
       }
   }

}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main(){
   ll n;
   cin >> n;
   ll sum = 0, max1 = 0, sum1 = 0, x;
   for(ll i = 0; i < n; ++i){

       ll t, c;
       cin >> t >> c;
       if(i == 0){
           x = t;
       }
       sum1 += c;
       sum += c;
       if(i != 0)
       sum --;
       max1 = max(sum , max1);

   }
   cout << x + sum1 << " " <<max1;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main(){
   ll n, first, last, sum, maximum;
   cin >> n;
   ll a[1000001] = {0};
   for(ll i = 0; i < n; ++i){
       ll t, c;
       cin >> t >> c;
       a[t] = c;
       if(i == 0){
           first = t;
           sum = c;
           maximum = c;
       }
       if(i == (n - 1)){
           last = t;
       }
   }
   for(ll i = first + 1; i <= 1000000000; ++i){
       if(sum > 0)
       sum --;
       if(i <= last)
       sum += a[i];
       maximum = max(sum, maximum);
       if(i >= last){
           cout << i +  sum << " " << maximum ;
           break;
       }
   }

}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main(){
   ll n, first, last, sum, maximum;
   cin >> n;
   ll a[10000001] = {0};
   for(ll i = 0; i < n; ++i){
       ll t, c;
       cin >> t >> c;
       a[t] = c;
       if(i == 0){
           first = t;
           sum = c;
           maximum = c;
       }
       if(i == (n - 1)){
           last = t;
       }
   }
   for(ll i = first + 1; i <= 1000000000; ++i){
       if(sum > 0)
       sum --;
       if(i <= last)
       sum += a[i];
       maximum = max(sum, maximum);
       if(sum == 0 && i >= last){
           cout << i % 1000000000 << " " << maximum % 1000000000;
           break;
       }
   }

}


#include<bits/stdc++.h>
using namespace std;
#define maxn (int)1e5+1000
#define up(i,x,y) for(int i=x;i<=y;i++)
#define down(i,x,y) for(int i=x;i>=y;i--)
#define bug cout<<"-----------"<<endl
#define pb push_back
typedef long long ll;

int main()
{
    int n;scanf("%d",&n);

    int cur = 0;
    int max_info = -1, tmp_info = 0;

    while(n --)
    {
        int t,c;scanf("%d %d",&t,&c);
        int e = t - cur;
        cur = t;
        tmp_info -= e;
        tmp_info = max(tmp_info,0);
        tmp_info += c;

        max_info = max(max_info, tmp_info);
    }
    cur += tmp_info;

    cout << cur << ' ' << max_info << endl;
}


#include <iostream>
#include <bits/stdc++.h>
#include <iomanip>
typedef long long int lli;
#define FAST ios_base::sync_with_stdio(false);
#define FASTT cin.tie(NULL);
#define pb push_back
#define pf push_front
#define mp make_pair
#define ff first
#define ss second
#define ap append
using namespace std;

int main(){
    FAST;FASTT;
    int s=0,n,t,c,hold=1,ans=0;
    cin>>n;
    while(n--){
        cin>>t>>c;
        if(s>0){
            s=max(0,s-(t-hold));
        }
        hold=t;
        s+=c;
        ans=max(s,ans);
    }
    cout<<t+s<<" "<<ans<<endl;
}


#include <bits/stdc++.h>
#define fr first
#define sc scanf
#define pf printf
#define se second
#define ll long long
#define pb push_back
#define fin(s) freopen( s, "r", stdin );
#define fout(s) freopen( s, "w", stdout );
#define TIME ios_base::sync_with_stdio(0)
using namespace std;

const ll INF = 1e9;
const ll N = 1e5 + 1;
const ll mod = 1e8 + 0;
const long double eps = 1E-7;

ll n, x, y;
ll mx, cr, ed;
int main()
{
    TIME;
    cin >> n;
    for( int i = 1; i <= n; i ++ )
    {
        cin >> x >> y;
        if( x <= ed )
            cr = ed - x + y;
        else
            cr = false;
        mx = max( mx , cr );
        mx = max( mx , y );
        ed = max( ed , x ) + y;
    }
    cout << ed << " " << mx << endl;
}


#include<bits/stdc++.h>
#define vi vector<int>
#define pii pair<int, int>

#define maxn 100010
#define maxlogn 20

using namespace std;

vi mix( vi & a, vi & b ){
	vi r;
	for(int i = 0, j = 0; r.size() < 10; ){
		if( i == a.size() && j == b.size() ) break;
		else if( i == a.size() ) r.push_back(b[j++]);
		else if( j == b.size() ) r.push_back(a[i++]);
		else if( a[i] <= b[j] )r.push_back(a[i++]);
		else if( b[j] < a[i] ) r.push_back(b[j++]);
	}
	return r;
}

class BinaryLifting{ public:
	int table[maxn][maxlogn];
	vi table_pessoas[maxn][maxlogn];
	vi pessoas[maxn];
	int prof[maxn];
	int tam;
	BinaryLifting(){
		memset(table, -1, sizeof table);
	}
	// a profundidade
	// a posição [x][0] deve está preenchida.(Os pais dos nós)
	void build(int n){
		int i, j, aux;
		tam = n;

		for(i = 0; i < n; i++){
			for(j = 1; j < maxlogn; j++){
				aux = table[i][j-1];
				if( aux != -1 && table[aux][j-1] != -1 ){
					table_pessoas[i][j] = mix(table_pessoas[i][j-1], table_pessoas[aux][j-1]);
					table[i][j] = table[aux][j-1];
				}
			}
		}
	}

	int up(int a, int d, vi & r){
		for(int i = 0; i < maxlogn; i++){
			if( d & (1 << i) ){
				r = mix(r, table_pessoas[a][i]);
				a = table[a][i];
			}
		}
		return a;
	}

	vi lca(int a, int b){
		vi r;
		//cout << "X1" << endl;
		if( a == b ) return pessoas[a];
		if( prof[a] != prof[b] ){
			if( prof[a] < prof[b] ){
				r = pessoas[b];
				b = up(b, prof[b]-prof[a], r);
				if( a != b ){
					r = mix(r, pessoas[a]);
				}else return r;
			}
			if( prof[b] < prof[a] ){
				r = pessoas[a];
				a = up(a, prof[a]-prof[b], r);
				if( a != b ){
					r = mix(r, pessoas[b]);
				}else return r;
			}
		}else{
			r = mix(pessoas[a], pessoas[b]);
		}
		//cout << "X3" << endl;

		for(int i = maxlogn-1; i >= 0; i--){
			if( table[a][i] != table[b][i] ){
				r = mix(r, table_pessoas[a][i]);
				r = mix(r, table_pessoas[b][i]);
				a = table[a][i];
				b = table[b][i];
			}
		}

		return mix(r, table_pessoas[a][0]);
	}
} lca;

int n, m, q, a, b, x;
vi g[maxn];

void dfs(int v, int pai){
	lca.table[v][0] = pai;
	for(int i = 0; i < g[v].size(); i++){
		int u = g[v][i];
		if( pai != u ){
			lca.prof[u] = lca.prof[v] + 1;
			lca.table_pessoas[u][0] = lca.pessoas[v];

			dfs(u, v);
		}
	}
}

int main(){
	scanf(" %d %d %d", &n, &m, &q);

	for(int i = 0; i < n-1; i++){
		scanf(" %d %d", &a, &b); a--; b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}

	for(int i = 1; i <= m; i++){
		scanf(" %d", &a); a--;
		if( lca.pessoas[a].size() < 10 )
			lca.pessoas[a].push_back(i);
	}

	lca.prof[0] = 0;
	dfs(0, -1);

	lca.build(n);

	for(int i = 0; i < q; i++){
		scanf(" %d %d %d", &a, &b, &x); a--; b--;
		vi r = lca.lca(a, b);

		printf("%d", min((int)r.size(), x));
		for(int i = 0; i < r.size() && i < x; i++){
			printf(" %d", r[i]);
		}printf("\n");
	}
	return 0;
}


#include<bits/stdc++.h>
#define vi vector<int>
#define pii pair<int, int>

#define maxn 100010
#define maxlogn 20

using namespace std;

vi mix( vi & a, vi & b ){
	vi r;
	for(int i = 0, j = 0; r.size() < 10; ){
		if( i == a.size() && j == b.size() ) break;
		else if( i == a.size() ) r.push_back(b[j++]);
		else if( j == b.size() ) r.push_back(a[i++]);
		else if( a[i] <= b[j] )r.push_back(a[i++]);
		else if( b[j] < a[i] ) r.push_back(b[j++]);
	}
	return r;
}

class BinaryLifting{ public:
	int table[maxn][maxlogn];
	vi table_pessoas[maxn][maxlogn];
	vi pessoas[maxn];
	int prof[maxn];
	int tam;
	BinaryLifting(){
		memset(table, -1, sizeof table);
	}
	// a profundidade
	// a posição [x][0] deve está preenchida.(Os pais dos nós)
	void build(int n){
		int i, j, aux;
		tam = n;

		for(i = 0; i < n; i++){
			for(j = 1; j < maxlogn; j++){
				aux = table[i][j-1];
				if( aux != -1 && table[aux][j-1] != -1 ){
					table_pessoas[i][j] = mix(table_pessoas[i][j-1], table_pessoas[aux][j-1]);
					table[i][j] = table[aux][j-1];
				}
			}
		}
	}

	int up(int a, int d, vi & r){
		for(int i = 0; i < maxlogn; i++){
			if( d & (1 << i) ){
				r = mix(r, table_pessoas[a][i]);
				a = table[a][i];
			}
		}
		return a;
	}

	vi lca(int a, int b){
		vi r;
		//cout << "X1" << endl;
		if( a == b ) return pessoas[a];
		if( prof[a] != prof[b] ){
			if( prof[a] < prof[b] ){
				r = pessoas[b];
				b = up(b, prof[b]-prof[a], r);
				if( a != b ){
					r = mix(r, pessoas[a]);
				}else return r;
			}
			if( prof[b] < prof[a] ){
				r = pessoas[a];
				a = up(a, prof[a]-prof[b], r);
				if( a != b ){
					r = mix(r, pessoas[b]);
				}else return r;
			}
		}else{
			r = mix(pessoas[a], pessoas[b]);
		}
		//cout << "X3" << endl;

		for(int i = maxlogn-1; i >= 0; i--){
			if( table[a][i] != table[b][i] ){
				r = mix(r, table_pessoas[a][i]);
				r = mix(r, table_pessoas[b][i]);
				a = table[a][i];
				b = table[b][i];
			}
		}

		return mix(r, table_pessoas[a][0]);
	}

	void imprimir(){
		cout << "Tamanho: " << tam << endl;
		for(int i = 0; i < tam; i++){
			cout << "i(" << i << "): ";
			for(int j = 0; j < maxlogn; j++){
				cout << table[i][j] << " ";
			}
			cout << endl;
		}cout << endl;
	}
} lca;

int n, m, q, a, b, x;
pii consultas[maxn];
vi g[maxn];

void dfs(int v, int pai){
	lca.table[v][0] = pai;
	for(int i = 0; i < g[v].size(); i++){
		int u = g[v][i];
		if( pai != u ){
			lca.prof[u] = lca.prof[v] + 1;
			lca.table_pessoas[u][0] = lca.pessoas[v];

			dfs(u, v);
		}
	}
}

int main(){
	cin >> n >> m >> q;

	for(int i = 0; i < n-1; i++){
		cin >> a >> b; a--; b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}

	for(int i = 1; i <= m; i++){
		cin >> a; a--;
		lca.pessoas[a].push_back(i);
	}
	lca.prof[0] = 0;
	dfs(0, -1);

	lca.build(n);

	for(int i = 0; i < q; i++){
		cin >> a >> b >> x; a--; b--;
		vi r = lca.lca(a, b);

		cout << min((int)r.size(), x);
		for(int i = 0; i < r.size() && i < x; i++){
			cout << " " << r[i];
		}cout << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define vi vector<int>
#define pii pair<int, int>

#define maxn 50010
#define maxlogn 20

using namespace std;

vi mix( vi & a, vi & b ){
	vi r;
	for(int i = 0, j = 0; r.size() < 10; ){
		if( i == a.size() && j == b.size() ) break;
		else if( i == a.size() ) r.push_back(b[j++]);
		else if( j == b.size() ) r.push_back(a[i++]);
		else if( a[i] <= b[j] )r.push_back(a[i++]);
		else if( b[j] < a[i] ) r.push_back(b[j++]);
	}
	return r;
}

class BinaryLifting{ public:
	int table[maxn][maxlogn];
	vi table_pessoas[maxn][maxlogn];
	vi pessoas[maxn];
	int prof[maxn];
	int tam;
	BinaryLifting(){
		memset(table, -1, sizeof table);
	}
	// a profundidade
	// a posição [x][0] deve está preenchida.(Os pais dos nós)
	void build(int n){
		int i, j, aux;
		tam = n;

		for(i = 0; i < n; i++){
			for(j = 1; j < maxlogn; j++){
				aux = table[i][j-1];
				if( aux != -1 && table[aux][j-1] != -1 ){
					table_pessoas[i][j] = mix(table_pessoas[i][j-1], table_pessoas[aux][j-1]);
					table[i][j] = table[aux][j-1];
				}
			}
		}
	}

	int up(int a, int d, vi & r){
		for(int i = 0; i < maxlogn; i++){
			if( d & (1 << i) ){
				r = mix(r, table_pessoas[a][i]);
				a = table[a][i];
			}
		}
		return a;
	}

	vi lca(int a, int b){
		vi r;
		//cout << "X1" << endl;
		if( a == b ) return pessoas[a];
		if( prof[a] != prof[b] ){
			if( prof[a] < prof[b] ){
				r = pessoas[b];
				b = up(b, prof[b]-prof[a], r);
				if( a != b ){
					r = mix(r, pessoas[a]);
				}else return r;
			}
			if( prof[b] < prof[a] ){
				r = pessoas[a];
				a = up(a, prof[a]-prof[b], r);
				if( a != b ){
					r = mix(r, pessoas[b]);
				}else return r;
			}
		}else{
			r = mix(pessoas[a], pessoas[b]);
		}
		//cout << "X3" << endl;

		for(int i = maxlogn-1; i >= 0; i--){
			if( table[a][i] != table[b][i] ){
				r = mix(r, table_pessoas[a][i]);
				r = mix(r, table_pessoas[b][i]);
				a = table[a][i];
				b = table[b][i];
			}
		}

		return mix(r, table_pessoas[a][0]);
	}

	void imprimir(){
		cout << "Tamanho: " << tam << endl;
		for(int i = 0; i < tam; i++){
			cout << "i(" << i << "): ";
			for(int j = 0; j < maxlogn; j++){
				cout << table[i][j] << " ";
			}
			cout << endl;
		}cout << endl;
	}
} lca;

int n, m, q, a, b, x;
pii consultas[maxn];
vi g[maxn];

void dfs(int v, int pai){
	lca.table[v][0] = pai;
	for(int i = 0; i < g[v].size(); i++){
		int u = g[v][i];
		if( pai != u ){
			lca.prof[u] = lca.prof[v] + 1;
			lca.table_pessoas[u][0] = lca.pessoas[v];

			dfs(u, v);
		}
	}
}

int main(){
	cin >> n >> m >> q;

	for(int i = 0; i < n-1; i++){
		cin >> a >> b; a--; b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}

	for(int i = 1; i <= m; i++){
		cin >> a; a--;
		lca.pessoas[a].push_back(i);
	}
	lca.prof[0] = 0;
	dfs(0, -1);

	lca.build(n);

	for(int i = 0; i < q; i++){
		cin >> a >> b >> x; a--; b--;
		vi r = lca.lca(a, b);

		cout << min((int)r.size(), x);
		for(int i = 0; i < r.size() && i < x; i++){
			cout << " " << r[i];
		}cout << endl;
	}
	return 0;
}