1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
// Generated by include.py
// Jakub Staroń, 2016
// For more info see https://github.com/staronj/ProgrammingContestsLibrary




#include <iostream>
#include <sstream>
#include <iterator>
#include <type_traits>
#include <algorithm>
#include <memory>
#include <utility>
#include <functional>
#include <bitset>
#include <vector>
#include <array>
#include <list>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cinttypes>
#include <ctime>
#include <cctype>
#include <cstring>
#include <random>
#include <cassert>

namespace lib {

using int8    = std::int8_t;
using uint8   = std::uint8_t;
using int16   = std::int16_t;
using uint16  = std::uint16_t;
using int32   = std::int32_t;
using uint32  = std::uint32_t;
using int64   = std::int64_t;
using uint64  = std::uint64_t;


std::knuth_b Random(time(0));


uint32 Random32() {
  return Random();
}


uint64 Random64() {
  return uint64(Random32()) << 32 | uint64(Random32());
}

#ifdef __SIZEOF_INT128__
#define HAVE_INT128_TYPES
#endif

#define USE_INT128_TYPES_IF_AVAILABLE 0

#if defined(HAVE_INT128_TYPES) && USE_INT128_TYPES_IF_AVAILABLE
#define USE_INT128_TYPES
#endif

#ifdef USE_INT128_TYPES
using int128 = __int128;
using uint128 = unsigned __int128;
#endif

using bit_vector = std::vector<bool>;

using char_pair     = std::pair<char, char>;
using bool_pair     = std::pair<bool, bool>;
using int32_pair    = std::pair<int32, int32>;
using uint32_pair   = std::pair<uint32, uint32>;
using int64_pair    = std::pair<int64, int64>;
using uint64_pair   = std::pair<uint64, uint64>;

} 





namespace lib {


template <typename T>
inline T operator++(T& obj, int) {
  T temp = obj;
  ++obj;
  return temp;
}


template <typename T>
inline T operator--(T& obj, int) {
  T temp = obj;
  --obj;
  return temp;
}


template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T&>::type
operator+=(T& obj, Integral n) {
  obj = obj + n;
  return obj;
}


template <typename T>
inline T& operator+=(T& obj1, const T& obj2) {
  obj1 = obj1 + obj2;
  return obj1;
}


template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T&>::type
operator-=(T& obj, Integral n) {
  obj = obj - n;
  return obj;
}


template <typename T>
inline T& operator-=(T& obj1, const T& obj2) {
  obj1 = obj1 - obj2;
  return obj1;
}


template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T&>::type
operator*=(T& obj, Integral n) {
  obj = obj * n;
  return obj;
}


template <typename T>
inline T& operator*=(T& obj1, const T& obj2) {
  obj1 = obj1 * obj2;
  return obj1;
}


template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T&>::type
operator%=(T& obj, Integral n) {
  obj = obj % n;
  return obj;
}


template <typename T>
inline T& operator%=(T& obj1, const T& obj2) {
  obj1 = obj1 % obj2;
  return obj1;
}


template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T>::type
operator+(Integral n, const T& obj) {
  return obj + n;
}


template <typename T, typename Integral>
inline typename std::enable_if<std::is_integral<Integral>::value, T>::type
operator*(Integral n, const T& obj) {
  return obj * n;
}


template <typename T, typename Integral>
inline typename std::enable_if<std::is_signed<Integral>::value, T>::type
operator-(const T& obj, Integral n) {
  return obj + (-n);
}


template <typename T, typename Integral>
inline typename std::enable_if<std::is_signed<Integral>::value, T&>::type
operator-=(const T& obj, Integral n) {
  return obj += (-n);
}


template <typename T>
inline bool operator>(const T& lhs, const T& rhs) {
  return rhs < lhs;
}


template <typename T>
inline bool operator<=(const T& lhs, const T& rhs) {
  return !(rhs < lhs);
}


template <typename T>
inline bool operator>=(const T& lhs, const T& rhs) {
  return rhs <= lhs;
}


template <typename T>
inline bool operator!=(const T& lhs, const T& rhs) {
  return !(lhs == rhs);
}


template <typename Integral1, typename Integral2>
constexpr std::pair<Integral1, Integral2> operator+(const std::pair<Integral1, Integral2>& lhs, const std::pair<Integral1, Integral2>& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first + rhs.first, lhs.second + rhs.second);
}


template <typename Integral1, typename Integral2>
constexpr std::pair<Integral1, Integral2> operator-(const std::pair<Integral1, Integral2>& lhs, const std::pair<Integral1, Integral2>& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first - rhs.first, lhs.second - rhs.second);
}


template <typename Integral1, typename Integral2>
constexpr std::pair<Integral1, Integral2> operator%(const std::pair<Integral1, Integral2>& lhs, const std::pair<Integral1, Integral2>& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first % rhs.first, lhs.second % rhs.second);
}


template <typename Integral1, typename Integral2>
constexpr std::pair<Integral1, Integral2> operator*(const std::pair<Integral1, Integral2>& lhs, const std::pair<Integral1, Integral2>& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first * rhs.first, lhs.second * rhs.second);
}


template <typename Integral1, typename Integral2, typename Integral3>
constexpr std::pair<Integral1, Integral2> operator%(const std::pair<Integral1, Integral2>& lhs, const Integral3& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first % rhs, lhs.second % rhs);
}


template <typename Integral1, typename Integral2, typename Integral3>
constexpr std::pair<Integral1, Integral2> operator*(const std::pair<Integral1, Integral2>& lhs, const Integral3& rhs) {
  return std::pair<Integral1, Integral2>(lhs.first * rhs, lhs.second * rhs);
}


} 

namespace lib {


template<typename Iterator>
struct is_iterator {
private:
  template<typename I> static constexpr auto test(int)
  -> decltype(
      *std::declval<const I>(),
      std::declval<const I>() == std::declval<const I>(),
      std::declval<const I>() != std::declval<const I>(),
      ++ std::declval<I&>(),
      std::declval<I&>() ++,
      std::iterator_traits<I>(),
      bool()) { return
        std::is_destructible<I>::value;
  }

  template<typename I> static constexpr bool test(...) { return false; }

public:
  static constexpr bool value = test<Iterator>(0);
};


template<typename T>
struct is_iterable {
  template <typename C>
  using begin_t = decltype(std::begin(std::declval<C&>()));

  template <typename C>
  using end_t = decltype(std::end(std::declval<C&>()));

  template<typename I> static constexpr auto test(int)
  -> decltype(
      std::declval<begin_t<I>>(),
      std::declval<end_t<I>>(),
      bool()) { return
        std::is_same<begin_t<I>, end_t<I>>::value &&
        is_iterator<begin_t<I>>::value;
  }

  template<typename I> static constexpr bool test(...) { return false; }

public:
  static constexpr bool value = test<T>(0);
};


template <typename Integral>
class counting_iterator {
public:
  static_assert(std::is_integral<Integral>::value, "counting_iterator's template argument should be integral");
  using self_type = counting_iterator;
  using value_type = Integral;
  using reference = const value_type&;
  using pointer = const value_type*;
  using difference_type = typename std::make_signed<Integral>::type;
  using iterator_category = std::random_access_iterator_tag;

  counting_iterator(): value_() { }

  explicit counting_iterator(Integral n): value_(n) { }

  reference operator*() const {
    return value_;
  }

  self_type& operator++() {
    ++value_;
    return *this;
  }

  self_type& operator--() {
    --value_;
    return *this;
  }

  self_type& operator+=(difference_type n) {
    value_ += n;
    return *this;
  }

  friend self_type operator+(const self_type& obj, difference_type n) {
    return self_type(obj.value_ + n);
  }

  friend difference_type operator-(const self_type& lhs, const self_type& rhs) {
    return lhs.value_ - rhs.value_;
  }

  value_type operator[](difference_type n) {
    return value_type(value_ + n);
  }

  friend bool operator<(const self_type& lhs, const self_type& rhs) {
    return lhs.value_ < rhs.value_;
  }

  friend bool operator==(const self_type& lhs, const self_type& rhs) {
    return lhs.value_ == rhs.value_;
  }

private:
  Integral value_;
};


template <typename Integral>
auto make_counting_iterator(Integral n) -> counting_iterator<Integral> {
  return counting_iterator<Integral>(n);
}


template <typename Integral>
class reverse_counting_iterator {
public:
  static_assert(std::is_integral<Integral>::value, "reverse_counting_iterator's template argument should be integral");
  using self_type = reverse_counting_iterator;
  using value_type = Integral;
  using reference = const value_type&;
  using pointer = const value_type*;
  using difference_type = typename std::make_signed<Integral>::type;
  using iterator_category = std::random_access_iterator_tag;

  reverse_counting_iterator(): value_() { }

  explicit reverse_counting_iterator(Integral n): value_(n) { }

  reference operator*() const {
    return value_;
  }

  self_type& operator++() {
    --value_;
    return *this;
  }

  self_type& operator--() {
    ++value_;
    return *this;
  }

  self_type& operator+=(difference_type n) {
    value_ -= n;
    return *this;
  }

  friend self_type operator+(const self_type& obj, difference_type n) {
    return self_type(obj.value_ - n);
  }

  friend difference_type operator-(const self_type& lhs, const self_type& rhs) {
    return rhs.value_ - lhs.value_;
  }

  value_type operator[](difference_type n) {
    return value_type(value_ - n);
  }

  friend bool operator<(const self_type& lhs, const self_type& rhs) {
    return rhs.value_ < lhs.value_;
  }

  friend bool operator==(const self_type& lhs, const self_type& rhs) {
    return lhs.value_ == rhs.value_;
  }

private:
  Integral value_;
};


template <typename Integral>
auto make_reverse_counting_iterator(Integral n) -> reverse_counting_iterator<Integral> {
  return reverse_counting_iterator<Integral>(n);
}


template <typename Iterator>
class iterator_range {
public:
  using iterator_type = Iterator;
  using reference = const Iterator&;

  static_assert(is_iterator<Iterator>::value, "iterator_range's template argument should be iterator!");

  explicit iterator_range(Iterator begin, Iterator end):
      begin_(std::move(begin)), end_(std::move(end)) { }

  reference begin() const {
    return begin_;
  }

  reference end() const {
    return end_;
  }

private:
  iterator_type begin_;
  iterator_type end_;
};


template <typename Iterator>
auto make_range(Iterator begin, Iterator end) -> iterator_range<Iterator> {
  return iterator_range<Iterator>(std::move(begin), std::move(end));
}


template <typename Integral>
auto range(Integral begin, Integral end) ->
typename std::enable_if<std::is_integral<Integral>::value, iterator_range<counting_iterator<Integral>>>::type {
  return make_range(make_counting_iterator(begin), make_counting_iterator(end));
}


template <typename Integral>
auto rrange(Integral begin, Integral end) ->
typename std::enable_if<std::is_integral<Integral>::value, iterator_range<reverse_counting_iterator<Integral>>>::type {
  return make_range(make_reverse_counting_iterator(end - 1), make_reverse_counting_iterator(begin - 1));
}


template <typename Iterator, typename Mapper>
class mapping_iterator {
public:
  using self_type = mapping_iterator;
  using underlying_value_type = decltype(*std::declval<Iterator>());
  using value_type = decltype(std::declval<Mapper>()(std::declval<underlying_value_type>()));
  using reference = value_type;
  using pointer = typename std::remove_reference<value_type>::type*;
  using difference_type = typename std::iterator_traits<Iterator>::difference_type;
  using iterator_category = typename std::iterator_traits<Iterator>::iterator_category;

  mapping_iterator(const Mapper& mapper = Mapper()):
      iterator_(), mapper_(mapper) { }

  explicit mapping_iterator(Iterator iterator, const Mapper& mapper = Mapper()):
      iterator_(iterator), mapper_(mapper) { }

  reference operator*() const {
    return mapper_(*iterator_);
  }

  self_type& operator++() {
    ++iterator_;
    return *this;
  }

  self_type& operator--() {
    --iterator_;
    return *this;
  }

  self_type& operator+=(difference_type n) {
    iterator_ += n;
    return *this;
  }

  friend self_type operator+(const self_type& obj, difference_type n) {
    return self_type(obj.iterator_ + n, obj.mapper_);
  }

  friend difference_type operator-(const self_type& lhs, const self_type& rhs) {
    return lhs.iterator_ - rhs.iterator_;
  }

  value_type operator[](difference_type n) {
    return *(*this + n);
  }

  friend bool operator<(const self_type& lhs, const self_type& rhs) {
    return lhs.iterator_ < rhs.iterator_;
  }

  friend bool operator==(const self_type& lhs, const self_type& rhs) {
    return lhs.iterator_ == rhs.iterator_;
  }

private:
  Iterator iterator_;
  Mapper mapper_;
};

namespace detail {

template <typename ValuesIterator>
struct IndirectMapper {
  static_assert(is_iterator<ValuesIterator>::value, "first indirect_iterator's template argument must be iterator.");
  static_assert(
      std::is_same<typename std::iterator_traits<ValuesIterator>::iterator_category, std::random_access_iterator_tag>::value,
      "ValuesIterator must be random assess iterator."
  );

  using value_type = typename std::iterator_traits<ValuesIterator>::value_type;
  using reference = const value_type&;
  using index_type = typename std::iterator_traits<ValuesIterator>::difference_type;

  IndirectMapper() = default;

  IndirectMapper(ValuesIterator values):
      values_(values) { }

  reference operator()(index_type n) const {
    return *(values_ + n);
  }

private:
  ValuesIterator values_;
};

} 

template <typename ValuesIterator, typename IndexesIterator>
using indirect_iterator = mapping_iterator<IndexesIterator, detail::IndirectMapper<ValuesIterator>>;


template <typename ValuesIterator, typename IndexesIterator>
auto make_indirect_iterator(ValuesIterator values, IndexesIterator indexes) ->
indirect_iterator<ValuesIterator, IndexesIterator> {
  detail::IndirectMapper<ValuesIterator> mapper(values);
  return indirect_iterator<ValuesIterator, IndexesIterator>(std::move(indexes), std::move(mapper));
};


template <typename ValueType>
class generator {
public:
  using ptr = std::shared_ptr<generator>;
  using value_type = ValueType;

  generator() = default;
  generator(const generator&) = delete;
  generator& operator=(const generator&) = delete;
  generator(generator&&) = default;
  virtual ~generator() = default;

  
  virtual value_type next() = 0;

  
  virtual bool hasNext() = 0;
};


template <typename ValueType>
class generator_iterator {
public:
  using self_type = generator_iterator;
  using generator_type = generator<ValueType>;
  using generator_pointer = typename generator<ValueType>::ptr;
  using value_type = typename generator_type::value_type;
  using reference = const value_type&;
  using pointer = const value_type*;
  using difference_type = std::ptrdiff_t;
  using iterator_category = std::input_iterator_tag;

  generator_iterator() = default;

  generator_iterator(generator_type* generator):
      generator_iterator(generator_pointer(generator)) { }

  generator_iterator(generator_pointer generator):
      generator_(std::move(generator)) {
    advance();
  }

  reference operator*() const {
    return value_;
  }

  pointer operator->() const {
    return &value_;
  }

  self_type& operator++() {
    advance();
    return *this;
  }

  friend bool operator==(const self_type& lhs, const self_type& rhs) {
    return (&lhs == &rhs) || (lhs.is_end() && rhs.is_end());
  }

  bool is_end() const {
    return !bool(generator_.get());
  }

private:
  void advance() {
    if (generator_->hasNext())
      value_ = generator_->next();
    else
      generator_.reset();
  }

  generator_pointer generator_;
  value_type value_;
};


template <typename T>
iterator_range<generator_iterator<T>> iterate_generator(generator<T>* gen) {
  return make_range(generator_iterator<T>(gen), generator_iterator<T>());
}

} 





namespace lib {

namespace detail {

const int kSimpleFancyFlagID = std::ios_base::xalloc();

enum printing_type {
  simple_printing_type = 0,
  fancy_printing_type = 1
};

class delimiter_printer {
public:
  delimiter_printer(std::ostream& stream): first_(true) {
    fancy_ = (stream.iword(kSimpleFancyFlagID) == fancy_printing_type);
  }

  const char* prefix() {
    return fancy_? "(" : "";
  }

  const char* delimiter() {
    if (!first_) {
      return fancy_? ", " : " ";
    }
    else {
      first_ = false;
      return "";
    }
  }

  const char* postfix() {
    return fancy_? ")" : "";
  }

private:
  bool first_;
  bool fancy_;
};

template <typename T>
constexpr bool allow_print_operator() {
  return
      is_iterable<T>::value &&
      !std::is_same<T, std::string>::value &&
      !std::is_same<T, const char*>::value &&
      !std::is_same<typename std::remove_extent<T>::type, char>::value;
}

} 


std::ostream& simple(std::ostream& stream) {
  stream.iword(detail::kSimpleFancyFlagID) = detail::simple_printing_type;
  return stream;
}


std::ostream& fancy(std::ostream& stream) {
  stream.iword(detail::kSimpleFancyFlagID) = detail::fancy_printing_type;
  return stream;
}


template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& stream, const std::pair<T1, T2>& pair);


template <typename... Args>
std::ostream& operator<<(std::ostream& stream, const std::tuple<Args...>& tuple);


template <typename Iterable>
typename std::enable_if<detail::allow_print_operator<Iterable>(), std::ostream&>::type
operator<<(std::ostream& stream, const Iterable& iterable);

#ifdef USE_INT128_TYPES


std::ostream& operator<<(std::ostream& stream, uint128 n) {
  constexpr int32 buffer_size = 64;
  constexpr uint128 ten = 10;
  char buffer[buffer_size];
  int32 index = buffer_size - 1;

  do {
    uint128 digit = n % 10;
    buffer[index--] = char('0' + digit);
    n /= 10;
  } while (n > 0 && index >= 0);

  stream.write(buffer + index + 1, buffer_size - index - 1);
  return stream;
}


std::ostream& operator<<(std::ostream& stream, int128 n) {
  if (n < 0) {
    stream.put('-');
    n = -n;
  }
  stream << uint128(n);
  return stream;
}

#endif


template <typename T1, typename T2>
std::istream& operator>>(std::istream& stream, std::pair<T1, T2>& pair);


template <typename... Args>
std::istream& operator>>(std::istream& stream, std::tuple<Args...>& tuple);


template <typename T>
struct ignore {
  ignore() = default;

  friend std::istream& operator>>(std::istream& stream, const ignore&&) {
    T ignored;
    return stream >> ignored;
  }

  friend std::istream& operator>>(std::istream& stream, const ignore&) {
    T ignored;
    return stream >> ignored;
  }
};

namespace detail {

template<std::size_t...>
struct integer_sequence{};

template <size_t N>
struct generate_sequence {
private:
  template<std::size_t M, std::size_t... Is>
  struct helper {
    using type = typename helper<M-1, M-1, Is...>::type;
  };;

  template<std::size_t... Is>
  struct helper<0, Is...> {
    using type = integer_sequence<Is...>;
  };

public:
  using type = typename helper<N>::type;
};

template <typename Functor, size_t N>
struct dynamize {
  using functor_type = Functor;
private:
  using index_sequence = typename generate_sequence<N>::type;
  using function_type = void(functor_type::*)();
  using table_type = std::array<function_type, N>;

  template <size_t... Indexes>
  static constexpr table_type build_table(integer_sequence<Indexes...>) {
    return {{&functor_type::template operator()<Indexes>...}};
  }

  static constexpr table_type functions_ = build_table(index_sequence());
public:
  dynamize(functor_type&& functor):
      functor_(std::move(functor)) { }

  void call(size_t i) {
    if (i >= N)
      throw std::out_of_range("tuple_printer: out of range");

    (functor_.*functions_[i])();
  }

private:
  functor_type functor_;
};

template <typename Functor, size_t N>
constexpr typename dynamize<Functor, N>::table_type dynamize<Functor, N>::functions_;

template <typename... Args>
class tuple_printer {
  static constexpr size_t arguments_count = sizeof...(Args);
  using tuple_type = std::tuple<Args...>;

  struct impl {
    std::ostream& stream_;
    const tuple_type& tuple_;

    template <size_t N>
    void operator()() {
      stream_ << std::get<N>(tuple_);
    }
  };

public:
  tuple_printer (std::ostream& stream, const tuple_type& tuple):
    dynamize_(impl{stream, tuple}) { }

  void print(size_t i) {
    dynamize_.call(i);
  }

private:
  dynamize<impl, arguments_count> dynamize_;
};

template <typename... Args>
class tuple_reader {
  static constexpr size_t arguments_count = sizeof...(Args);
  using tuple_type = std::tuple<Args...>;

  struct impl {
    std::istream& stream_;
    tuple_type& tuple_;

    template <size_t N>
    void operator()() {
      stream_ >> std::get<N>(tuple_);
    }
  };

public:
  tuple_reader (std::istream& stream, tuple_type& tuple):
      dynamize_(impl{stream, tuple}) { }

  void read(size_t i) {
    dynamize_.call(i);
  }

private:
  dynamize<impl, arguments_count> dynamize_;
};

} 

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& stream, const std::pair<T1, T2>& pair) {
  detail::delimiter_printer printer(stream);
  stream << printer.prefix();
  stream << printer.delimiter() << pair.first;
  stream << printer.delimiter() << pair.second;
  stream << printer.postfix();
  return stream;
}

template <typename... Args>
std::ostream& operator<<(std::ostream& stream, const std::tuple<Args...>& tuple) {
  detail::delimiter_printer delimiter_printer(stream);
  detail::tuple_printer<Args...> tuple_printer(stream, tuple);
  stream << delimiter_printer.prefix();
  for (auto i: range<size_t>(0, sizeof...(Args))) {
    stream << delimiter_printer.delimiter();
    tuple_printer.print(i);
  }
  stream << delimiter_printer.postfix();
  return stream;
}

template <typename Iterable>
typename std::enable_if<detail::allow_print_operator<Iterable>(), std::ostream&>::type
operator<<(std::ostream& stream, const Iterable& iterable) {
  detail::delimiter_printer printer(stream);
  stream << printer.prefix();
  for (const auto& elem: iterable) {
    stream << printer.delimiter() << elem;
  }
  stream << printer.postfix();
  return stream;
}

template <typename T1, typename T2>
std::istream& operator>>(std::istream& stream, std::pair<T1, T2>& pair) {
  return stream >> pair.first >> pair.second;
}

template <typename... Args>
std::istream& operator>>(std::istream& stream, std::tuple<Args...>& tuple) {
  detail::tuple_reader<Args...> tuple_reader(stream, tuple);
  for (auto i: range<size_t>(0, sizeof...(Args))) {
    tuple_reader.read(i);
  }
  return stream;
}



template <typename... Args>
std::istream& operator>>(std::istream& stream, std::tuple<Args...>&& tuple) {
  return stream >> tuple;
}


template <typename... Args>
void print(std::ostream& stream, const char* format, const Args&... args) {
  auto tuple = std::make_tuple(std::cref(args)...);
  detail::tuple_printer<const Args&...> tuple_printer(stream, tuple);
  constexpr char null = '\0';
  constexpr char percent = '%';
  for (const char* it = format, *prev = format; *it != '\0'; ) {
    while (*it != null && *it != percent)
      it++;

    stream.write(prev, it - prev);
    if (*it == percent) {
      ++it;
      if (*it == percent) {
        stream.put(percent);
      }
      else if (std::isdigit(*it)) {
        size_t index = size_t(*it - '0');
        tuple_printer.print(index);
      }
      else {
        throw std::invalid_argument("print - invalid character after %");
      }
      prev = ++it;
    }
  }
  stream.put('\n');
}


template <typename... Args>
void print(const char* format, const Args&... args) {
  print(std::cout, format, args...);
}


std::ostream& flush(std::ostream& stream) {
  return stream.flush();
}


std::ostream& newline(std::ostream& stream) {
  return stream.put('\n');
}


template <typename... Args>
void read(std::istream& stream, Args&&... args) {
  auto tuple = std::make_tuple(std::ref(args)...);
  stream >> tuple;
}


class lines_generator: public generator<std::string> {
public:
  lines_generator(std::istream* stream):
      stream_(stream) { }

  value_type next() final {
    if (!stream_->good())
      throw std::ios::failure("lines_generator - stream in invalid state after line read.");
    else
      std::getline(*stream_, line_);
    return line_;
  }

  bool hasNext() final {
    return !stream_->eof();
  }

private:
  std::istream* stream_;
  std::string line_;
};


class lines_iterator: public generator_iterator<std::string> {
public:
  lines_iterator() = default;

  lines_iterator(std::istream* stream):
      generator_iterator(new lines_generator(stream)) { }
};


iterator_range<lines_iterator> iterate_lines(std::istream& stream) {
  return make_range(lines_iterator(&stream), lines_iterator());
}

} 

namespace lib {
namespace logging {

enum class log_level {
  debug = 15,
  info = 10,
  error = 5,
  none = 0
};

class Logger {
public:
  friend Logger& get_logger(const std::string& name);

  Logger(Logger&& other):
      level_(other.level_),
      streams_(std::move(other.streams_)) { }

  Logger(const Logger&) = delete;
  Logger& operator=(const Logger&) = delete;

  template <typename... Args>
  void log(log_level level, const char* format, const Args&... args) {
    if (level > level_)
      return;

    for (const auto& pair: streams_) {
      if (level > pair.second)
        continue;

      print(*pair.first, format, args...);
    }
  }

  template <typename... Args>
  void debug(const char* format, const Args&... args) {
    log(log_level::debug, format, args...);
  }

  template <typename... Args>
  void info(const char* format, const Args&... args) {
    log(log_level::info, format, args...);
  }

  template <typename... Args>
  void error(const char* format, const Args&... args) {
    log(log_level::error, format, args...);
  }

  void add_stream(std::ostream* stream, log_level level) {
    streams_.emplace_back(stream, level);
  }

  void set_log_level(log_level level) {
    level_ = level;
  }

protected:
  Logger() = default;

private:
  log_level level_ = log_level::debug;
  std::vector<std::pair<std::ostream*, log_level>> streams_;
};

Logger& get_logger(const std::string& name) {
  static std::map<std::string, Logger> loggers;
  auto it = loggers.find(name);

  if (it == loggers.end()) {
    it = loggers.emplace(name, Logger()).first;
  }
  return it->second;
}

} 
} 

using namespace lib;

logging::Logger& logger = logging::get_logger("grz");


template<class T>
class StaticBinaryTree {
public:
  typedef T node_type;
  typedef StaticBinaryTree<node_type> basic_type;
  typedef uint32 index_type;

  uint32 real_size() {
    return data_size * 2;
  }

  uint32 size() {
    return data_size;
  }

protected:
  StaticBinaryTree(uint32 n) {
    uint32 PowerOfTwo = 8;
    while (PowerOfTwo < n)
      PowerOfTwo <<= 1;

    data_size = PowerOfTwo;
    tree.resize(data_size * 2);
  }

  bool is_left_son(index_type k) {
    return (k % 2 == 0);
  }

  bool is_right_son(index_type k) {
    return (k % 2 == 1);
  }

  index_type left_sibling(index_type k) {
    return (k - k % 2);
  }

  index_type right_sibling(index_type k) {
    return (k - k % 2 + 1);
  }

  index_type parent(index_type i) {
    return i / 2;
  }

  index_type left_son(index_type i) {
    return i * 2;
  }

  index_type right_son(index_type i) {
    return i * 2 + 1;
  }

  index_type index_of_node(uint32 k) {
    return k + data_size;
  }

  index_type root() {
    return 1;
  }

  bool is_leaf(index_type n) {
    return (n >= data_size && n < data_size * 2);
  }

  void assert_range(uint32 first, uint32 last) {
    if (first > last)
      throw std::logic_error("StaticBinaryTree: first > last!");
    else if (last >= size())
      throw std::logic_error("StaticBinaryTree: last >= size()!");
  }

  void assert_index(uint32 index) {
    if (index >= size())
      throw std::logic_error("StaticBinaryTree: index >= size()");
  }

  std::vector<node_type> tree;
private:
  uint32 data_size;
};

struct TreeNode {
  int64 A = 0;
  int64 B = 0;
};

class IntervalAddAddTree : public StaticBinaryTree<TreeNode> {
public:
  typedef int64 value_type;

  IntervalAddAddTree(uint32 n) : basic_type(n) {
  }

  void insert(uint32 first, uint32 last, value_type value) {
    first = index_of_node(first);
    last = index_of_node(last);

    uint32 length = 1;

    tree[first].A += value;
    UpdateNode(first, length);

    if (first != last) {
      tree[last].A += value;
      UpdateNode(last, length);
    }

    while (first > 0) {
      if (first + 1 < last) {
        if (is_left_son(first)) {
          uint32 sibling = right_sibling(first);
          tree[sibling].A += value;
          UpdateNode(sibling, length);
        }

        if (is_right_son(last)) {
          uint32 sibling = left_sibling(last);
          tree[sibling].A += value;
          UpdateNode(sibling, length);
        }
      }

      UpdateNode(first, length);
      UpdateNode(last, length);

      first = parent(first);
      last = parent(last);
      length *= 2;
    }
  }

  value_type query(uint32 first, uint32 last) {
    first = index_of_node(first);
    last = index_of_node(last);

    uint32 length = 1;

    uint32 left_length = 1, right_length = ((first == last) ? 0 : 1);
    value_type result = 0;

    while (first > 0) {
      result += tree[first].A * left_length + tree[last].A * right_length;

      if (first + 1 < last) {
        if (is_left_son(first)) {
          uint32 sibling = right_sibling(first);
          result += tree[sibling].B;
          left_length += length;
        }

        if (is_right_son(last)) {
          uint32 sibling = left_sibling(last);
          result += tree[sibling].B;
          right_length += length;
        }
      }

      first = parent(first);
      last = parent(last);
      length *= 2;
    }
    return result;
  }

  void UpdateNode(uint32 index, uint32 length) {
    if (is_leaf(index)) {
      tree[index].B = tree[index].A;
    } else {
      tree[index].B = tree[left_son(index)].B + tree[right_son(index)].B + tree[index].A * length;
    }
  }
};

class Application {
public:
  void Run() {
    NSquare();
  }

  void NSquare() {
    std::sort(glades.begin(), glades.end());

    int64 result = 0;
    int64 asSum = 0;
    bit_vector chosen(N, false);
    for (auto i: range<uint32>(0, N)) {
      int64 bestReward = -1;
      uint32 bestRewardIndex = 0;
      int64 asSumLoop = asSum;
      int64 index = 0;

      for (auto j: range<uint32>(0, N)) {
        if (chosen[j]) {
          index++;
          asSumLoop -= glades[j].first;
        }
        else {
          int64 reward = glades[j].second + asSumLoop + index * glades[j].first;
          if (bestReward < reward) {
            bestReward = reward;
            bestRewardIndex = j;
          }
        }
      }

      result += bestReward;
      chosen[bestRewardIndex] = true;
      asSum += glades[bestRewardIndex].first;

      print("%0", result);
    }
  }

  void LoadData() {
    read(std::cin, N);
    glades.resize(N);
    for (auto i: range<uint32>(0, N)) {
      read(std::cin, glades[i]);
    }
  }

private:
  uint32 N;
  std::vector<int64_pair> glades;
};

int main(int argc, const char* argv[]) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  logger.set_log_level(logging::log_level::info);
  logger.add_stream(&std::cerr, logging::log_level::debug);
  Application application;
  application.LoadData();
  application.Run();
  return 0;
}