QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#117946#3226. Distance Sumi_am_noob#AC ✓834ms50644kbC++143.3kb2023-07-02 17:19:542023-07-02 17:19:57

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-02 17:19:57]
  • 评测
  • 测评结果:AC
  • 用时:834ms
  • 内存:50644kb
  • [2023-07-02 17:19:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
const int mod = 1000'000'007, N = 200005, M = 18;
int add(int x, int y){x+=y; if(x>=mod) x-=mod; return x;}
int sub(int x, int y){x-=y; if(x<0) x+=mod; return x;}
int mul(int x, int y){return ((ll)x)*y%mod;}
int Pow(int x, ll y=mod-2){int res=1; for(; y; x=mul(x,x),y>>=1) if(y&1) res=mul(res,x); return res;}

int n,tl[N],tr[N],siz[N],par[N][M],par2[N],cnt2[N];
ll dis[N],sum2[N],sum3[N];
vector<int> adj[N];
bool vis2[N];

void dfs1(int u){
    static int t=-1;
    tl[u]=++t;
    if(par[u][0]>=0) dis[u]+=dis[par[u][0]];
    for(int i=1; i<M; ++i) par[u][i]=par[u][i-1]==-1?-1:par[par[u][i-1]][i-1];
    for(auto v: adj[u]) if(v!=par[u][0]) dfs1(v);
    tr[u]=t;
}

bool is_anc(int u, int v){return tl[u]<=tl[v]&&tr[u]>=tr[v];}

int lca(int u, int v){
    if(is_anc(u,v)) return u;
    for(int i=M-1; i>=0; --i) if(par[u][i]>=0&&!is_anc(par[u][i],v)) u=par[u][i];
    return par[u][0];
}

ll get_dis(int u, int v){return dis[u]+dis[v]-2*dis[lca(u,v)];}

void dfs2(int u, int fa){
    siz[u]=1;
    for(auto v: adj[u]) if(!vis2[v]&&v!=fa) dfs2(v,u),siz[u]+=siz[v];
}

int findG(int u, int fa, int m){
    for(auto v: adj[u]) if(!vis2[v]&&v!=fa&&siz[v]>m/2) return findG(v,u,m);
    return u;
}

void centroid(int u, int fa){
    dfs2(u,-1);
    int g=findG(u,-1,siz[u]);
    vis2[g]=1,par2[g]=fa;
    for(auto v: adj[g]) if(!vis2[v]) centroid(v,g);
}

void add(int u){
    for(int i=u; i>=0; i=par2[i]){
        cnt2[i]++;
        if(par2[i]>=0){
            ll tmp=get_dis(u,par2[i]);
            sum2[par2[i]]+=tmp,sum3[i]+=tmp;
        }
    }
}

ll query(int u){
    ll res=sum2[u];
    for(int i=u; par2[i]>=0; i=par2[i]){
        res+=(cnt2[par2[i]]-cnt2[i])*get_dis(u,par2[i])+(sum2[par2[i]]-sum3[i]);
        //cout << "res " << (cnt2[par2[i]]-cnt2[i]) << ' ' << get_dis(u,par2[i]) << ' ' << (sum2[par2[i]]-sum3[i]) << ' ' << res << endl;
    }
    return res;
}

int bit[N];
void add_bit(int u){for(int i=tl[u]+1; i<N; i+=i&-i) bit[i]++;}
int query_bit(int u){
    int res=0;
    for(int i=tr[u]+1; i; i-=i&-i) res+=bit[i];
    for(int i=tl[u]; i; i-=i&-i) res-=bit[i];
    return res;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    par[0][0]=-1;
    for(int i=1; i<n; ++i){
        cin >> par[i][0] >> dis[i];
        par[i][0]--;
        adj[par[i][0]].pb(i),adj[i].pb(par[i][0]);
    }
    dfs1(0);
    centroid(0,-1);
    //for(int i=0; i<n; ++i) cout << par2[i] << ' ';
    //cout << endl;
    //cout << dis[1] << ' ' << dis[2] << ' ' << get_dis(1,2) << endl;
    int cur=0;
    for(int i=0; i<n; ++i){
        add(i),add_bit(i);
        for(int j=M-1; j>=0; --j) if(par[cur][j]>=0&&query_bit(par[cur][j])<=(i+1)/2) cur=par[cur][j];
        if(query_bit(cur)<=(i+1)/2) cur=par[cur][0];
        if(is_anc(cur,i)){
            int cur2=i;
            for(int j=M-1; j>=0; --j) if(par[cur2][j]>=0&&query_bit(par[cur2][j])<=(i+1)/2) cur2=par[cur2][j];
            if(query_bit(cur2)<=(i+1)/2) cur2=par[cur2][0];
            cur=cur2;
        }
        //cout << i << ' ' << cur << endl;
        cout << query(cur) << "\n";
    }
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 17792kb

input:

10
5 1
2 1
1 1
4 1
2 1
5 1
2 1
2 1
5 1

output:

0
3
4
6
6
8
9
11
12
14

result:

ok 10 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 17912kb

input:

10
5 1
10 1
5 1
3 1
5 1
5 1
1 1
3 1
1 1

output:

0
4
4
6
6
7
8
12
14
16

result:

ok 10 lines

Test #3:

score: 0
Accepted
time: 2ms
memory: 17804kb

input:

10
4 1
7 1
9 1
3 1
7 1
1 1
7 1
6 1
3 1

output:

0
5
6
9
11
12
12
13
15
17

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 2ms
memory: 17796kb

input:

10
1 1
9 1
9 1
6 1
9 1
3 1
10 1
1 1
3 1

output:

0
1
3
5
7
8
10
13
13
15

result:

ok 10 lines

Test #5:

score: 0
Accepted
time: 3ms
memory: 18100kb

input:

10
6 1
6 1
3 1
3 1
1 1
1 1
9 1
3 1
1 1

output:

0
2
3
5
6
7
9
12
13
16

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 4ms
memory: 17956kb

input:

10
8 1
10 1
6 1
3 1
1 1
1 1
4 1
5 1
4 1

output:

0
4
6
6
9
10
13
14
18
19

result:

ok 10 lines

Test #7:

score: 0
Accepted
time: 2ms
memory: 17972kb

input:

10
5 1
8 1
9 1
1 1
1 1
5 1
5 1
1 1
1 1

output:

0
2
4
7
7
9
10
11
13
15

result:

ok 10 lines

Test #8:

score: 0
Accepted
time: 3ms
memory: 17756kb

input:

10
5 1
5 1
8 1
4 1
5 1
4 1
1 1
2 1
7 1

output:

0
4
5
6
6
7
9
11
13
16

result:

ok 10 lines

Test #9:

score: 0
Accepted
time: 0ms
memory: 17724kb

input:

10
4 1
1 1
3 1
3 1
5 1
6 1
6 1
8 1
4 1

output:

0
3
3
4
5
7
10
13
16
19

result:

ok 10 lines

Test #10:

score: 0
Accepted
time: 1ms
memory: 17756kb

input:

10
8 1
10 1
8 1
4 1
9 1
10 1
1 1
8 1
8 1

output:

0
2
4
5
7
9
11
11
12
13

result:

ok 10 lines

Test #11:

score: 0
Accepted
time: 227ms
memory: 38212kb

input:

200000
109891 65231
171839 5776
32431 29819
62570 71905
153470 68881
188361 76298
151469 77636
75162 130242
95864 47113
191182 742
121927 111781
18165 51034
158645 36001
193496 189958
143195 29723
140274 86466
117583 23287
184465 144332
35935 128306
192514 116854
27679 197718
138926 123165
46773 177...

output:

0
2128476
3615067
4333135
5793589
7104606
7876598
9621410
10959705
12443304
12989537
15017823
15660417
16551255
18007974
18487621
20027188
21203385
22454974
22931660
23796204
25173856
26195907
27604778
28662852
29446260
30856590
32645490
33338651
34979442
36020161
37417274
38263592
39224218
40460394...

result:

ok 200000 lines

Test #12:

score: 0
Accepted
time: 826ms
memory: 45392kb

input:

200000
196697 155143
178488 159177
49016 18473
171950 94863
69271 100194
160889 20733
40420 19766
191138 127108
142060 2610
194472 41608
6942 105158
61037 3025
150904 197834
58272 30296
5859 116513
104509 1986
48140 173435
131223 123177
175812 40180
28608 98965
157005 67994
127784 114441
17436 68620...

output:

0
11320917384
11874614449
15772614655
18523775058
21957641652
30009559722
31970095838
35324661901
38253652284
48493111245
51933582990
58121449414
67628507769
73905238960
83828871781
83828871781
86351127383
86906418351
88386492876
89035727731
94945127602
103238157194
103565667357
106031510890
1148471...

result:

ok 200000 lines

Test #13:

score: 0
Accepted
time: 43ms
memory: 38428kb

input:

200000
100271 58998
100271 15879
100271 138986
100271 113904
100271 115478
100271 83571
100271 27243
100271 160493
100271 9252
100271 48723
100271 192210
100271 94228
100271 34372
100271 164043
100271 117906
100271 65950
100271 3526
100271 27894
100271 100392
100271 125787
100271 41701
100271 73898
...

output:

0
184014
199893
338879
452783
568261
651832
679075
839568
848820
897543
1089753
1183981
1218353
1382396
1500302
1566252
1569778
1597672
1698064
1823851
1865552
1939450
1985877
2053259
2186511
2309128
2336836
2417401
2514921
2533909
2641402
2645106
2837716
2867548
3041880
3119887
3270987
3470933
3530...

result:

ok 200000 lines

Test #14:

score: 0
Accepted
time: 386ms
memory: 38028kb

input:

200000
74397 4664
156428 79828
33782 165515
92020 132016
88427 42599
80143 31404
154122 97504
38283 40204
163610 54024
103901 41100
56105 102790
713 129668
123254 101263
66304 117240
193266 30852
106010 58682
58420 44200
195244 150716
69688 108732
66176 90998
186655 161913
46056 19248
55107 2538
117...

output:

0
9321877
61989505
120141123
169103327
213727540
253084679
305259586
324006109
346052256
356850684
398366900
426326820
463012059
500894672
510409428
533431749
582755784
617910987
676094992
691040861
739632515
788623478
824330028
863333091
908581077
940061483
958059386
990954065
1042453000
1078844771...

result:

ok 200000 lines

Test #15:

score: 0
Accepted
time: 113ms
memory: 38900kb

input:

200000
54924 186089
104599 54897
71905 93151
55761 58428
42708 143931
31004 47401
41379 77312
196586 168160
184380 6392
28713 115038
43040 194521
96586 4585
88667 72708
136090 40925
782 79526
66341 34788
118013 129857
169548 2564
175295 152197
69866 53074
15750 57176
70527 197060
57173 85109
17214 3...

output:

0
952477
1697204
2306850
3143286
4169881
4386998
4911577
5720878
6277669
6841135
7401750
8493012
9198085
9929087
10814018
11030933
11362776
11703065
12540120
13281681
13608438
14204796
14826878
15583588
16325692
17061805
18015452
18691433
19142053
20393462
21385448
21760815
22449760
23224178
2398148...

result:

ok 200000 lines

Test #16:

score: 0
Accepted
time: 204ms
memory: 38184kb

input:

200000
34874 1
57858 1
47062 1
62655 1
97660 1
126570 1
48620 1
98264 1
17460 1
58592 1
158887 1
49817 1
181633 1
115252 1
45450 1
39671 1
62069 1
64096 1
70903 1
128135 1
17797 1
49999 1
7116 1
58905 1
48870 1
80175 1
132057 1
88936 1
31073 1
177648 1
144876 1
162626 1
26871 1
22093 1
160575 1
9982...

output:

0
18
27
36
48
52
69
81
90
101
120
125
131
145
151
168
177
188
202
217
229
242
252
260
273
287
303
313
323
334
351
360
369
380
395
406
416
424
433
443
461
474
486
498
508
521
533
556
565
579
590
601
610
627
648
655
665
680
691
698
715
726
742
751
770
781
794
803
816
824
837
844
854
865
880
893
907
92...

result:

ok 200000 lines

Test #17:

score: 0
Accepted
time: 834ms
memory: 48916kb

input:

200000
125885 1
37417 1
89954 1
83141 1
148186 1
183613 1
41748 1
111572 1
7829 1
163199 1
75242 1
102308 1
58808 1
97180 1
118265 1
180798 1
93765 1
71643 1
162564 1
99982 1
41014 1
154491 1
71796 1
52767 1
46881 1
88636 1
67646 1
43151 1
135777 1
50186 1
36420 1
191350 1
92492 1
173451 1
138222 1
...

output:

0
41163
153861
217804
233549
341919
420127
445418
504740
607104
607104
699373
699373
715767
767826
791818
859734
891184
930766
991338
1094753
1102574
1149717
1170000
1254176
1272935
1344097
1389416
1437545
1509176
1559137
1626288
1655613
1720144
1805338
1872975
1925812
1962257
1975601
2001239
205585...

result:

ok 200000 lines

Test #18:

score: 0
Accepted
time: 47ms
memory: 37808kb

input:

200000
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
112666 1
11266...

output:

0
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
...

result:

ok 200000 lines

Test #19:

score: 0
Accepted
time: 379ms
memory: 38028kb

input:

200000
149039 1
99120 1
80039 1
20811 1
162948 1
62163 1
194453 1
197332 1
19488 1
145549 1
48954 1
84036 1
62638 1
153117 1
162399 1
160932 1
48215 1
10809 1
113214 1
114941 1
22980 1
29236 1
1600 1
192646 1
34825 1
47420 1
54304 1
59087 1
27885 1
42851 1
75734 1
166846 1
146206 1
87844 1
89589 1
4...

output:

0
313
378
1218
1504
2080
2423
2614
3068
3572
3762
4034
4138
4262
4441
5030
5290
5638
5944
6579
6773
6846
7428
7631
8019
8452
8987
9493
9692
9970
10523
10796
11029
11707
11716
12118
12684
12760
12889
13167
13255
13566
14205
14212
14234
14884
15477
15709
15889
15988
16439
16869
17351
17662
17753
18051...

result:

ok 200000 lines

Test #20:

score: 0
Accepted
time: 92ms
memory: 38852kb

input:

200000
47219 1
181329 1
169467 1
173660 1
142954 1
4472 1
72219 1
52843 1
57535 1
104614 1
186911 1
61814 1
80201 1
165638 1
98832 1
50920 1
128320 1
143337 1
138310 1
13971 1
142954 1
104339 1
43664 1
42634 1
85643 1
71368 1
67407 1
71738 1
134555 1
154651 1
22781 1
187307 1
97390 1
63496 1
195823 ...

output:

0
8
15
23
31
34
40
53
57
65
69
73
79
82
89
95
101
108
111
115
119
121
126
133
139
144
149
153
158
167
173
179
185
191
199
203
211
217
223
231
238
243
251
256
263
275
281
289
294
297
303
306
310
315
323
329
338
344
350
357
365
370
376
384
389
396
403
412
420
425
433
441
451
458
463
468
475
481
489
49...

result:

ok 200000 lines

Test #21:

score: 0
Accepted
time: 214ms
memory: 38268kb

input:

200000
27017 67865
31903 55381
78332 78682
199117 87857
151381 25977
14445 28652
114012 66198
4486 45610
119719 83126
138799 61385
52358 29047
138739 32470
170017 16963
28620 21735
21615 99226
186586 34582
178194 30365
155257 18756
32578 47139
43649 83316
154287 75562
14222 57131
61671 72132
110169 ...

output:

0
965765
1490236
2438956
3340313
3882869
4301685
5049403
5714210
6926221
7567451
8016113
8553088
9031795
9322836
10037626
10391992
10727237
11508829
11882699
12372807
13026818
13758947
14206785
14952639
15187514
15919358
16746726
17584436
18270035
18905223
19555561
19792137
20300220
21039036
2180730...

result:

ok 200000 lines

Test #22:

score: 0
Accepted
time: 802ms
memory: 45396kb

input:

200000
58291 17971
41992 91355
13137 57742
41696 95200
175861 21616
9032 35003
69662 76709
133061 56323
117018 26316
77826 81608
175523 101819
186846 82356
121403 92216
54201 43367
134303 94463
25956 80557
149964 58216
33690 56958
14550 21453
107520 10486
25601 60718
14773 16489
128961 27483
87564 6...

output:

0
4144150978
4144150978
7030452173
9546822292
10220745154
12094932210
14289698911
19896509986
26042504932
29988354801
31277561236
34395323366
39138078358
39138078358
40493188383
42602724728
44773651370
47801582038
50197637958
54967389733
56836621748
58844088612
61396789431
65165460079
70331006684
73...

result:

ok 200000 lines

Test #23:

score: 0
Accepted
time: 58ms
memory: 38392kb

input:

200000
158335 24362
158335 5223
158335 22382
158335 7090
158335 13562
158335 14118
158335 13867
158335 2093
158335 15571
158335 18285
158335 1424
158335 19149
158335 13718
158335 19872
158335 16281
158335 6993
158335 20770
158335 19024
158335 23741
158335 12230
158335 16201
158335 22866
158335 16186...

output:

0
26297
31520
53902
60992
74554
88672
102539
104632
120203
138488
139912
159061
172779
192651
208932
215925
236695
255719
279460
291690
307891
330757
346943
349945
374528
396859
417527
430468
444132
456660
480585
503997
512997
536056
556472
572953
581735
590871
611165
626258
636354
651150
663951
671...

result:

ok 200000 lines

Test #24:

score: 0
Accepted
time: 429ms
memory: 38092kb

input:

200000
106973 117689
136282 104481
157728 121145
40992 143143
177913 36725
65799 41920
160294 67530
24733 137870
10749 55847
81124 112472
186440 162476
26111 50431
154962 44266
28230 44840
103482 83016
148578 157009
156973 50602
139704 6401
76847 50747
11663 40727
163853 67281
99481 47637
79743 1119...

output:

0
46001526
51338783
72714744
87770984
92800178
111834549
122617088
172921447
181612276
215564546
242712752
283849480
309803373
331278182
346873133
373112990
397608327
425236415
452954170
473297289
499158809
506954110
531626611
553318188
560451926
571311934
621339382
632946096
667732219
710624333
735...

result:

ok 200000 lines

Test #25:

score: 0
Accepted
time: 104ms
memory: 39056kb

input:

200000
87053 25421
33728 4053
117211 2701
41029 28489
98103 82743
180939 14427
38799 36645
43498 6317
127478 11580
73813 4025
178654 56094
157543 12219
34553 33070
100972 604
17519 56247
117211 26160
66735 18826
120432 11241
64918 78892
157722 81226
148088 64729
196196 29636
777 61100
26482 21167
24...

output:

0
467263
515147
1034644
1572717
1775616
2283006
2535775
2865424
3306914
3554928
3931349
4213470
4597249
4748449
5309104
5692161
6127384
6565135
6844438
7267460
7629495
7873941
8223350
8344824
8501546
8984806
9068118
9160601
9597652
9809875
10079687
10417749
10907627
11238295
11499498
11803301
120831...

result:

ok 200000 lines

Test #26:

score: 0
Accepted
time: 20ms
memory: 21044kb

input:

25631
21450 71389
4384 185819
13938 73226
10298 28327
6031 64405
1019 192433
2339 82109
15989 179967
18264 63176
20052 155114
4164 100775
22691 136723
6774 159607
22691 150388
17938 111332
12335 67921
13423 59112
16829 135436
13724 114673
8096 177048
22084 174753
14709 121606
2459 161775
12441 59515...

output:

0
2436590
3723118
5082750
5743865
6489707
6910216
7714413
9042516
9679697
10716614
12015891
13122259
13620916
14740949
15279360
16115301
16676030
17354930
18757199
19466542
20671932
21431091
22970798
24131584
25437233
27131332
28124234
29039481
29545451
30410075
30979951
31830130
33829588
34637480
3...

result:

ok 25631 lines

Test #27:

score: 0
Accepted
time: 247ms
memory: 29124kb

input:

74776
49637 160138
1118 962
10378 25216
30499 168538
37978 179482
12694 134881
7257 83093
66821 38250
26100 164166
10706 31241
44474 54582
45344 63174
70833 6439
6465 82925
4643 50357
9627 73667
22350 118424
45805 52179
46609 138630
31298 186022
7480 150517
23245 10064
31841 46078
56727 51751
57366 ...

output:

0
2470619009
2470619009
7587846411
8322604953
10465486902
10490432042
12554884800
14850277095
15107690912
15885601039
18613296268
19891658749
22495600860
24308761679
24444653297
24663021315
27103157659
28173072018
29218862618
29268586782
32465821228
35278751829
38475874802
40575937021
41475680753
43...

result:

ok 74776 lines

Test #28:

score: 0
Accepted
time: 5ms
memory: 19900kb

input:

29565
5431 36552
5431 76628
5431 164555
5431 172471
5431 192742
5431 111601
5431 64499
5431 11956
5431 11153
5431 176155
5431 160827
5431 85779
5431 103861
5431 29591
5431 189346
5431 100691
5431 16627
5431 9322
5431 6010
5431 86035
5431 71452
5431 27614
5431 118437
5431 187797
5431 19894
5431 71869...

output:

0
193941
270569
435124
607595
800337
911938
976437
988393
999546
1175701
1336528
1422307
1526168
1555759
1745105
1845796
1862423
1871745
1877755
1963790
2035242
2062856
2181293
2369090
2388984
2460853
2551118
2575882
2730815
2899219
3017136
3181565
3223487
3286768
3481081
3537514
3676066
3780999
380...

result:

ok 29565 lines

Test #29:

score: 0
Accepted
time: 174ms
memory: 27784kb

input:

97426
24157 168297
2724 152694
8251 2800
85577 198128
1 33231
5720 98859
85908 107003
29976 104499
18088 171916
91589 153559
16663 106503
40903 157643
84352 68825
55250 134123
79553 96289
27735 671
54103 88676
37066 80985
5825 148004
40799 161467
3832 55306
41326 31190
21534 132544
19472 78414
31156...

output:

0
3439870
59684685
126672843
130018991
174230404
178019509
237077827
243697166
274921328
304560628
331584031
355418497
377694384
394506153
400265647
414992887
434286641
440976059
461141223
498018336
521097887
539253034
581831618
585144527
608436062
626395265
655235808
671393225
715978787
732704530
7...

result:

ok 97426 lines

Test #30:

score: 0
Accepted
time: 30ms
memory: 24424kb

input:

56584
4944 127922
28324 121537
47666 82922
25540 99609
24026 13846
22177 180794
48310 144801
47076 82918
20194 126726
25438 133558
20816 1158
50016 81827
20194 112389
51229 19632
24026 114882
51229 115768
48940 197682
39588 131151
39570 75063
391 145688
55715 46978
25426 140003
48257 181442
42893 42...

output:

0
855750
990901
2324781
2618645
3345148
3989587
4771306
5338148
5659035
6194454
6980127
7497204
7803754
8217174
8850458
9360014
10186328
11016189
11266245
12259617
12696998
13594352
14380285
15269365
16268462
16746794
17295068
18033030
18804098
19794128
20043879
20730740
21825391
22370458
23151253
2...

result:

ok 56584 lines

Test #31:

score: 0
Accepted
time: 79ms
memory: 29956kb

input:

91260
42844 1
23258 1
25877 1
58483 1
64261 1
75525 1
35224 1
26566 1
57449 1
8773 1
53576 1
19974 1
13014 1
36107 1
5899 1
16962 1
30251 1
7142 1
5546 1
80110 1
10231 1
29270 1
61058 1
30238 1
9499 1
90721 1
36581 1
11779 1
6764 1
42510 1
59624 1
31939 1
86917 1
56625 1
57209 1
70619 1
65164 1
1732...

output:

0
14
26
43
54
68
79
92
108
120
134
147
161
174
187
197
208
225
232
240
253
261
272
280
293
303
320
332
343
351
362
379
385
399
414
427
447
457
473
485
498
509
520
534
548
560
565
579
593
609
622
636
644
654
659
670
675
683
691
704
711
718
725
735
745
753
762
770
785
800
809
824
839
851
859
867
877
8...

result:

ok 91260 lines

Test #32:

score: 0
Accepted
time: 272ms
memory: 30932kb

input:

89188
50195 1
64229 1
980 1
6308 1
32796 1
33770 1
41809 1
26507 1
32706 1
53540 1
82093 1
27551 1
65763 1
66039 1
78375 1
9257 1
34096 1
6768 1
10829 1
15965 1
13551 1
68634 1
57633 1
65727 1
53157 1
52846 1
58244 1
15045 1
44034 1
21265 1
71022 1
31521 1
30706 1
45695 1
73020 1
63737 1
23665 1
189...

output:

0
11940
35891
50705
82840
86473
93892
113431
143903
172280
174988
183151
189682
207288
245064
255183
296684
335602
337732
372312
394166
423219
443431
453565
472696
486660
494684
532870
545051
583433
583433
625715
665350
702492
710942
736017
742122
784142
809605
831443
873520
904082
931511
966657
983...

result:

ok 89188 lines

Test #33:

score: 0
Accepted
time: 11ms
memory: 20972kb

input:

25488
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 1
10110 ...

output:

0
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
...

result:

ok 25488 lines

Test #34:

score: 0
Accepted
time: 31ms
memory: 20988kb

input:

26956
294 1
16525 1
14607 1
2704 1
24882 1
557 1
20324 1
1257 1
14780 1
8403 1
14505 1
15078 1
9378 1
15711 1
9693 1
19210 1
7272 1
4690 1
10448 1
14058 1
4251 1
18355 1
25948 1
18705 1
16578 1
9704 1
11375 1
4215 1
13447 1
9356 1
22137 1
14651 1
24253 1
20262 1
20785 1
25417 1
23095 1
12270 1
1590 ...

output:

0
329
508
829
835
1281
1298
1501
1533
1680
1915
2127
2188
2378
2449
2574
2736
2887
3006
3099
3173
3402
3543
3613
3626
3685
3925
4006
4116
4218
4538
4557
4671
4692
4932
5204
5265
5320
5573
5600
5754
5834
5896
6131
6228
6504
6655
6874
6908
7135
7290
7321
7393
7639
7692
7812
8071
8224
8488
8543
8711
88...

result:

ok 26956 lines

Test #35:

score: 0
Accepted
time: 24ms
memory: 24680kb

input:

66154
50769 1
14906 1
5844 1
16961 1
51343 1
34348 1
15403 1
10907 1
409 1
14571 1
44563 1
53414 1
54799 1
26248 1
10844 1
55239 1
3677 1
58134 1
8981 1
25228 1
15403 1
59155 1
3754 1
37822 1
58975 1
60295 1
32314 1
28947 1
3754 1
940 1
20602 1
19727 1
11451 1
65377 1
34433 1
32450 1
31658 1
35505 1...

output:

0
14
19
23
30
37
43
52
58
61
70
80
83
91
96
103
106
114
123
135
141
148
157
166
173
179
185
195
200
209
216
228
238
248
256
261
266
275
283
292
298
304
309
314
321
325
335
347
354
360
364
373
380
384
395
403
411
417
419
425
432
440
451
457
465
474
479
489
500
506
511
518
526
534
541
545
552
553
559
...

result:

ok 66154 lines

Test #36:

score: 0
Accepted
time: 477ms
memory: 50572kb

input:

200000
3 73817
6 41666
1 47757
4 138194
7 115291
10 67257
5 122264
8 52220
11 56362
14 85594
9 63811
12 74710
15 169338
18 20642
13 65096
16 190825
19 184795
22 91065
17 15258
20 173260
23 137846
26 184050
21 95401
24 36636
27 90191
30 151879
25 9053
28 5634
31 151490
34 25374
29 127832
32 78130
35 ...

output:

0
19964045186
19964045186
39927968798
39927968798
59891712550
59891712550
79855218747
79855218747
99818605467
99818605467
119781872014
119781872014
139744978257
139744978257
159707850066
159707850066
179670510408
179670510408
199632970697
199632970697
219595166661
219595166661
239557129378
239557129...

result:

ok 200000 lines

Test #37:

score: 0
Accepted
time: 499ms
memory: 50644kb

input:

200000
3 80879
6 87703
1 103853
4 14222
7 156402
10 63702
5 48441
8 196533
11 120075
14 623
9 78590
12 58956
15 138893
18 124993
13 115605
16 141182
19 154026
22 122963
17 96212
20 105152
23 181225
26 43607
21 197960
24 41680
27 148079
30 169705
25 13498
28 79096
31 8889
34 31119
29 198892
32 154954...

output:

0
20020654027
20020654027
40041123322
40041123322
60061490692
60061490692
80081653219
80081653219
100101555511
100101555511
120121259138
120121259138
140140903186
140140903186
160160292736
160160292736
180179416111
180179416111
200198289248
200198289248
220216934270
220216934270
240235200107
2402352...

result:

ok 200000 lines

Test #38:

score: 0
Accepted
time: 4ms
memory: 16532kb

input:

10
4 1
1 1
3 1
3 1
5 1
6 1
6 1
8 1
4 1

output:

0
3
3
4
5
7
10
13
16
19

result:

ok 10 lines

Test #39:

score: 0
Accepted
time: 0ms
memory: 17980kb

input:

15
1 3
12 5
5 2
12 1
7 5
5 1
6 1
12 1
11 1
12 4
1 1
5 5
10 4
1 2

output:

0
3
9
13
14
21
22
29
31
37
41
41
47
56
59

result:

ok 15 lines