Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- /// from HUNG MINE with love <3
- typedef pair <long long, long long> ii;
- typedef pair <ii, long long> iii;
- struct st {
- long long s, pos;
- };
- st a[300001];
- long long n, t[300001], sum = 0;
- stack <ii> p;
- vector <iii> ans;
- bool cmp (st a, st b) {
- return a.s < b.s;
- }
- int main () {
- //freopen (".inp", "r", stdin);
- //freopen (".out", "w", stdout);
- ios_base :: sync_with_stdio (0);
- cin.tie (0);
- cout.tie (0);
- cin >> n;
- for (int i = 1; i <= n; ++ i) {
- cin >> a[i].s;
- a[i].pos = i;
- }
- sort (a + 1, a + n + 1, cmp);
- for (int i = 1; i <= n; ++ i) {
- cin >> t[i];
- }
- sort (t + 1, t + n + 1);
- for (int i = 1; i <= n; ++ i) {
- sum += t[i] - a[i].s;
- }
- if (sum != 0) {
- cout << "NO";
- }
- else {
- for (int i = 1; i <= n; ++ i) {
- //if (p.size ()) cout << p.top ().first << " " << p.top ().second << "\n";
- if (t[i] - a[i].s > 0) {
- p.push (ii (i, t[i] - a[i].s));
- }
- else if (t[i] - a[i].s < 0) {
- while (p.size ()) {
- if ((a[i].s - a[p.top ().first].s) >= 2 * p.top ().second) {//cerr << 1;
- ans.push_back (iii (ii (a[p.top ().first].pos, a[i].pos),p.top ().second));
- a[i].s -= p.top ().second;
- a[p.top ().first].s += p.top ().second;
- p.top ().second -= p.top ().second;
- }
- else {//cerr << 2;
- ans.push_back (iii (ii (a[p.top ().first].pos, a[i].pos),a[i].s - t[i]));
- a[p.top ().first].s += (a[i].s - t[i]);
- p.top ().second -= (a[i].s - t[i]);
- a[i].s -= (a[i].s - t[i]);
- }
- if (t[i] - a[i].s >= 0) break;
- if (p.top ().second == 0) p.pop ();
- }
- if (t[i] - a[i].s > 0) {
- p.push (ii (i, t[i] - a[i].s));
- }
- }
- }
- for (int i = 1; i <= n; ++ i) {
- if (a[i].s != t[i]) return cout << "NO", 0;
- }
- cout << "YES\n";
- cout << ans.size () << "\n";
- for (auto val : ans) {
- cout << val.first.first << " " << val.first.second << " " << val.second << "\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment