#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int A[1000005], B[1000005], C[1000005], n;
pii ans[1000005];
int ft1[1000005], ft2[1000005];
void upd(int ft[], int l, int r, int v){
for(;l<=n;l+=(l & -l))ft[l] += v;
r++;
for(;r<=n;r+=(r & -r))ft[r] -= v;
}
int qry(int ft[], int p){
int res = 0;
for(;p;p-=(p & -p))res += ft[p];
return res;
}
int fin = 0;
void solve(){
cin >> n;
for(int i=1;i<=n;i++)cin >> A[i];
for(int i=1;i<n;i++)cin >> B[i];
for(int i=1;i<n;i++)cin >> C[i];
stack <pi> st;
for(int i=1;i<n;i++){
int x = min(B[i], A[i]);
ans[i].fi = x;
B[i] -= x;
A[i] -= x;
x = min(B[i], A[i+1]);
ans[i].se.se = x;
B[i] -= x;
A[i+1] -= x;
if(C[i])st.push({C[i], i});
while(!st.empty() && B[i]){
pi tmp = st.top(); st.pop();
x = min(tmp.fi, B[i]);
B[i] -= x;
tmp.fi -= x;
fin += x;
ans[tmp.se].se.fi += x;
if(tmp.se < i)upd(ft1, tmp.se, i - 1, -x), upd(ft2, tmp.se + 1, i, x);
if(tmp.fi)st.push(tmp);
}
/*
for(int j=i;j>=1;j--){
if(!B[i])break;
x = min(B[i], C[j]);
C[j] -= x;
B[i] -= x;
ans[j].se.fi += x;
fin += x;
for(int k=j;k<i;k++)ans[k].se.se -= x, ans[k+1].fi += x;
}
*/
if(B[i]){
cout << "NO\n";
return;
}
}
for(int i=1;i<=n;i++)ans[i].fi += qry(ft2, i), ans[i].se.se += qry(ft1, i);
for(int i=1;i<=n;i++)if(ans[i].se.se < 0){cout << "NO"; return;}
cout << "YES\n" << fin << '\n';
for(int i=1;i<n;i++)cout << ans[i].fi << ' ' << ans[i].se.fi << ' ' << ans[i].se.se << '\n';
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
Main.cpp:80:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
80 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
194 ms |
23756 KB |
Output is correct |
5 |
Correct |
261 ms |
24560 KB |
Output is correct |
6 |
Correct |
199 ms |
47176 KB |
Output is correct |
7 |
Correct |
272 ms |
48552 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
389 ms |
53520 KB |
Output is correct |
11 |
Correct |
390 ms |
54876 KB |
Output is correct |
12 |
Correct |
472 ms |
68588 KB |
Output is correct |
13 |
Correct |
465 ms |
61364 KB |
Output is correct |
14 |
Correct |
179 ms |
23800 KB |
Output is correct |
15 |
Correct |
524 ms |
70616 KB |
Output is correct |
16 |
Correct |
478 ms |
70756 KB |
Output is correct |
17 |
Correct |
482 ms |
70812 KB |
Output is correct |
18 |
Correct |
527 ms |
70632 KB |
Output is correct |
19 |
Correct |
542 ms |
70596 KB |
Output is correct |
20 |
Correct |
474 ms |
70588 KB |
Output is correct |
21 |
Correct |
469 ms |
70476 KB |
Output is correct |
22 |
Correct |
498 ms |
67832 KB |
Output is correct |
23 |
Correct |
475 ms |
70796 KB |
Output is correct |
24 |
Correct |
469 ms |
69824 KB |
Output is correct |
25 |
Correct |
237 ms |
26624 KB |
Output is correct |
26 |
Correct |
236 ms |
25624 KB |
Output is correct |
27 |
Correct |
487 ms |
70656 KB |
Output is correct |
28 |
Correct |
480 ms |
68700 KB |
Output is correct |
29 |
Correct |
483 ms |
70260 KB |
Output is correct |
30 |
Correct |
482 ms |
70580 KB |
Output is correct |
31 |
Correct |
506 ms |
69212 KB |
Output is correct |
32 |
Correct |
481 ms |
70500 KB |
Output is correct |
33 |
Correct |
233 ms |
25156 KB |
Output is correct |
34 |
Correct |
245 ms |
25992 KB |
Output is correct |
35 |
Correct |
241 ms |
25292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
472 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
476 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
476 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
500 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
472 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
476 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
476 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
500 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
3 ms |
724 KB |
Output is correct |
39 |
Correct |
2 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
448 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
328 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
3 ms |
724 KB |
Output is correct |
49 |
Correct |
2 ms |
724 KB |
Output is correct |
50 |
Correct |
3 ms |
724 KB |
Output is correct |
51 |
Correct |
3 ms |
724 KB |
Output is correct |
52 |
Correct |
3 ms |
724 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
3 ms |
668 KB |
Output is correct |
55 |
Correct |
2 ms |
724 KB |
Output is correct |
56 |
Correct |
3 ms |
724 KB |
Output is correct |
57 |
Correct |
2 ms |
732 KB |
Output is correct |
58 |
Correct |
3 ms |
596 KB |
Output is correct |
59 |
Correct |
2 ms |
712 KB |
Output is correct |
60 |
Correct |
4 ms |
596 KB |
Output is correct |
61 |
Correct |
2 ms |
656 KB |
Output is correct |
62 |
Correct |
3 ms |
724 KB |
Output is correct |
63 |
Correct |
3 ms |
732 KB |
Output is correct |
64 |
Correct |
1 ms |
468 KB |
Output is correct |
65 |
Correct |
2 ms |
468 KB |
Output is correct |
66 |
Correct |
3 ms |
724 KB |
Output is correct |
67 |
Correct |
3 ms |
724 KB |
Output is correct |
68 |
Correct |
3 ms |
724 KB |
Output is correct |
69 |
Correct |
3 ms |
724 KB |
Output is correct |
70 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
194 ms |
23756 KB |
Output is correct |
5 |
Correct |
261 ms |
24560 KB |
Output is correct |
6 |
Correct |
199 ms |
47176 KB |
Output is correct |
7 |
Correct |
272 ms |
48552 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
389 ms |
53520 KB |
Output is correct |
11 |
Correct |
390 ms |
54876 KB |
Output is correct |
12 |
Correct |
472 ms |
68588 KB |
Output is correct |
13 |
Correct |
465 ms |
61364 KB |
Output is correct |
14 |
Correct |
179 ms |
23800 KB |
Output is correct |
15 |
Correct |
524 ms |
70616 KB |
Output is correct |
16 |
Correct |
478 ms |
70756 KB |
Output is correct |
17 |
Correct |
482 ms |
70812 KB |
Output is correct |
18 |
Correct |
527 ms |
70632 KB |
Output is correct |
19 |
Correct |
542 ms |
70596 KB |
Output is correct |
20 |
Correct |
474 ms |
70588 KB |
Output is correct |
21 |
Correct |
469 ms |
70476 KB |
Output is correct |
22 |
Correct |
498 ms |
67832 KB |
Output is correct |
23 |
Correct |
475 ms |
70796 KB |
Output is correct |
24 |
Correct |
469 ms |
69824 KB |
Output is correct |
25 |
Correct |
237 ms |
26624 KB |
Output is correct |
26 |
Correct |
236 ms |
25624 KB |
Output is correct |
27 |
Correct |
487 ms |
70656 KB |
Output is correct |
28 |
Correct |
480 ms |
68700 KB |
Output is correct |
29 |
Correct |
483 ms |
70260 KB |
Output is correct |
30 |
Correct |
482 ms |
70580 KB |
Output is correct |
31 |
Correct |
506 ms |
69212 KB |
Output is correct |
32 |
Correct |
481 ms |
70500 KB |
Output is correct |
33 |
Correct |
233 ms |
25156 KB |
Output is correct |
34 |
Correct |
245 ms |
25992 KB |
Output is correct |
35 |
Correct |
241 ms |
25292 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
3 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
344 KB |
Output is correct |
50 |
Correct |
2 ms |
468 KB |
Output is correct |
51 |
Correct |
2 ms |
468 KB |
Output is correct |
52 |
Correct |
2 ms |
468 KB |
Output is correct |
53 |
Correct |
2 ms |
468 KB |
Output is correct |
54 |
Correct |
2 ms |
468 KB |
Output is correct |
55 |
Correct |
2 ms |
468 KB |
Output is correct |
56 |
Correct |
1 ms |
472 KB |
Output is correct |
57 |
Correct |
2 ms |
468 KB |
Output is correct |
58 |
Correct |
2 ms |
468 KB |
Output is correct |
59 |
Correct |
2 ms |
468 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
2 ms |
476 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
64 |
Correct |
2 ms |
476 KB |
Output is correct |
65 |
Correct |
2 ms |
468 KB |
Output is correct |
66 |
Correct |
2 ms |
468 KB |
Output is correct |
67 |
Correct |
2 ms |
468 KB |
Output is correct |
68 |
Correct |
1 ms |
500 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
336 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
3 ms |
724 KB |
Output is correct |
74 |
Correct |
2 ms |
724 KB |
Output is correct |
75 |
Correct |
2 ms |
448 KB |
Output is correct |
76 |
Correct |
1 ms |
468 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
328 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
3 ms |
724 KB |
Output is correct |
84 |
Correct |
2 ms |
724 KB |
Output is correct |
85 |
Correct |
3 ms |
724 KB |
Output is correct |
86 |
Correct |
3 ms |
724 KB |
Output is correct |
87 |
Correct |
3 ms |
724 KB |
Output is correct |
88 |
Correct |
1 ms |
468 KB |
Output is correct |
89 |
Correct |
3 ms |
668 KB |
Output is correct |
90 |
Correct |
2 ms |
724 KB |
Output is correct |
91 |
Correct |
3 ms |
724 KB |
Output is correct |
92 |
Correct |
2 ms |
732 KB |
Output is correct |
93 |
Correct |
3 ms |
596 KB |
Output is correct |
94 |
Correct |
2 ms |
712 KB |
Output is correct |
95 |
Correct |
4 ms |
596 KB |
Output is correct |
96 |
Correct |
2 ms |
656 KB |
Output is correct |
97 |
Correct |
3 ms |
724 KB |
Output is correct |
98 |
Correct |
3 ms |
732 KB |
Output is correct |
99 |
Correct |
1 ms |
468 KB |
Output is correct |
100 |
Correct |
2 ms |
468 KB |
Output is correct |
101 |
Correct |
3 ms |
724 KB |
Output is correct |
102 |
Correct |
3 ms |
724 KB |
Output is correct |
103 |
Correct |
3 ms |
724 KB |
Output is correct |
104 |
Correct |
3 ms |
724 KB |
Output is correct |
105 |
Correct |
3 ms |
724 KB |
Output is correct |
106 |
Correct |
1 ms |
320 KB |
Output is correct |
107 |
Correct |
0 ms |
340 KB |
Output is correct |
108 |
Correct |
568 ms |
79080 KB |
Output is correct |
109 |
Correct |
648 ms |
84472 KB |
Output is correct |
110 |
Correct |
431 ms |
65668 KB |
Output is correct |
111 |
Correct |
470 ms |
68796 KB |
Output is correct |
112 |
Correct |
3 ms |
852 KB |
Output is correct |
113 |
Correct |
3 ms |
860 KB |
Output is correct |
114 |
Correct |
675 ms |
94652 KB |
Output is correct |
115 |
Correct |
633 ms |
99344 KB |
Output is correct |
116 |
Correct |
392 ms |
83876 KB |
Output is correct |
117 |
Correct |
462 ms |
81776 KB |
Output is correct |
118 |
Correct |
592 ms |
80344 KB |
Output is correct |
119 |
Correct |
172 ms |
26408 KB |
Output is correct |
120 |
Correct |
643 ms |
94736 KB |
Output is correct |
121 |
Correct |
687 ms |
94708 KB |
Output is correct |
122 |
Correct |
649 ms |
94668 KB |
Output is correct |
123 |
Correct |
639 ms |
94680 KB |
Output is correct |
124 |
Correct |
679 ms |
94820 KB |
Output is correct |
125 |
Correct |
633 ms |
94996 KB |
Output is correct |
126 |
Correct |
653 ms |
95440 KB |
Output is correct |
127 |
Correct |
598 ms |
100944 KB |
Output is correct |
128 |
Correct |
664 ms |
94832 KB |
Output is correct |
129 |
Correct |
713 ms |
96976 KB |
Output is correct |
130 |
Correct |
373 ms |
27736 KB |
Output is correct |
131 |
Correct |
359 ms |
26716 KB |
Output is correct |
132 |
Correct |
737 ms |
95108 KB |
Output is correct |
133 |
Correct |
441 ms |
73180 KB |
Output is correct |
134 |
Correct |
698 ms |
94204 KB |
Output is correct |
135 |
Correct |
697 ms |
94132 KB |
Output is correct |
136 |
Correct |
690 ms |
97200 KB |
Output is correct |
137 |
Correct |
737 ms |
94088 KB |
Output is correct |
138 |
Correct |
476 ms |
70976 KB |
Output is correct |
139 |
Correct |
350 ms |
26072 KB |
Output is correct |
140 |
Correct |
328 ms |
25352 KB |
Output is correct |