Submission #5929677


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using LL  = long long;
using VI  = vector<LL>;
using VVI = vector<VI>;
using VB  = vector<bool>;
using VS  = vector<string>;
using PII = pair<LL, LL>;
using VP  = vector<PII>;
#define PB           push_back
#define MP           make_pair
#define SZ(a)        LL((a).size())
#define EACH(x, c)   for (auto x : (c))
#define ALL(c)       (c).begin(), (c).end()
#define REVERSE(c)   reverse(ALL(c))
#define SORT(c)      stable_sort(ALL(c))
#define RSORT(c)     stable_sort((c).rbegin(), (c).rend())
#define FSORT(c)     stable_sort(ALL(c), [] (auto& x, auto& y) {return x.first < y.first;});
#define FRSORT(c)    stable_sort(ALL(c), [] (auto& x, auto& y) {return x.first > y.first;});
#define SSORT(c)     stable_sort(ALL(c), [] (auto& x, auto& y) {return x.second < y.second;});
#define SRSORT(c)    stable_sort(ALL(c), [] (auto& x, auto& y) {return x.second > y.second;});
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n)    FOR(i, 0, n)
#define $(x) {cout << #x << " = " << (x) << endl;}

int main() {
    LL N;
    cin >> N;
    VI plus, minus;
    REP(i, N) {
        LL a;
        cin >> a;
        if (a < 0) minus.PB(a);
        else plus.PB(a);
    }
    SORT(plus);
    RSORT(minus);

    VP trace;
    LL x, y;
    LL ans;
    LL mi = 0, pi = 0;
    if (SZ(minus) == 0) {
        trace.PB(MP(plus[0], plus[1]));
        ans = plus[0] - plus[1];
        if (N > 2) {
            for (LL i = 2; i < N - 1; ++i) {
                trace.PB(MP(ans, plus[i]));
                ans -= plus[i];
            }
            trace.PB(MP(plus[N - 1], ans));
            ans = plus[N - 1] - ans;
        }
    } else if (SZ(plus) == 0) {
        trace.PB(MP(minus[0], minus[1]));
        ans = minus[0] - minus[1];
        for (LL i = 2; i < N; ++i) {
            trace.PB(MP(ans, minus[i]));
            ans -= minus[i];
        }
    } else {
        ans = minus[0];
        for (LL i = 0; i < SZ(plus) - 1; ++i) {
            trace.PB(MP(ans, plus[i]));
            ans -= plus[i];
        }
        trace.PB(MP(plus[SZ(plus) - 1], ans));
        ans = plus[SZ(plus) - 1] - ans;
        for (LL i = 1; i < SZ(minus); ++i) {
            trace.PB(MP(ans, minus[i]));
            ans -= minus[i];
        }
    }

    cout << ans << endl;
    EACH(t, trace) {
        cout << t.first << " " << t.second << endl;
    }
	
    return 0;
}

Submission Info

Submission Time
Task C - Successive Subtraction
User yetnone
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2477 Byte
Status AC
Exec Time 200 ms
Memory 5224 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 26
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt AC 12 ms 640 KiB
02.txt AC 11 ms 640 KiB
03.txt AC 11 ms 640 KiB
04.txt AC 11 ms 640 KiB
05.txt AC 11 ms 640 KiB
06.txt AC 193 ms 4972 KiB
07.txt AC 195 ms 5000 KiB
08.txt AC 194 ms 4968 KiB
09.txt AC 188 ms 4584 KiB
10.txt AC 197 ms 5108 KiB
11.txt AC 194 ms 5052 KiB
12.txt AC 197 ms 5052 KiB
13.txt AC 190 ms 4068 KiB
14.txt AC 189 ms 4344 KiB
15.txt AC 200 ms 4348 KiB
16.txt AC 183 ms 4376 KiB
17.txt AC 195 ms 4652 KiB
18.txt AC 193 ms 5096 KiB
19.txt AC 192 ms 5096 KiB
20.txt AC 196 ms 5096 KiB
21.txt AC 197 ms 5096 KiB
22.txt AC 200 ms 5224 KiB
23.txt AC 195 ms 5224 KiB
24.txt AC 1 ms 256 KiB
s1.txt AC 1 ms 256 KiB
s2.txt AC 1 ms 256 KiB