Submission #5924914


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

int n, num;
vector<int> a, b, c;
vector<pii> vec;

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> num;
		if(num < 0)
			a.push_back(num);
		else if(num == 0)
			c.push_back(num);
		else
			b.push_back(num);
	}
	sort(a.begin(), a.end());
	sort(b.begin(), b.end(), greater<int>());
	if(a.empty() && b.empty()) {
		cout << "0\n";
		for (int i = 0; i < n - 1; i++)
			cout << 0 << ' ' << 0 << '\n';
		return 0;
	}
	if(a.empty()) {
		if(c.size())
			a = c;
		else {
			a.push_back(b.back());
			b.pop_back();
		}
	}
	else if(b.empty()) {
		if(c.size())
			b = c;
		else {
			b.push_back(a.back());
			a.pop_back();
		}
	}
	else {
		for (int num: c)
			a.push_back(num);
	}
	
	for (int i = 0; i < n - 1; i++) {
		if(a.size() < b.size()) {
			vec.push_back(pii(a.back(), b.back()));
			int num = a.back() - b.back();
			a.pop_back(), b.pop_back();
			a.push_back(num);
		}
		else {
			vec.push_back(pii(b.back(), a.back()));
			int num = b.back() - a.back();
			a.pop_back(), b.pop_back();
			b.push_back(num);
		}
	}
	cout << b.back() << '\n';
	for (pii p: vec)
		cout << p.first << ' ' << p.second << '\n';
	return 0;
}

Submission Info

Submission Time
Task C - Successive Subtraction
User LODB
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1317 Byte
Status AC
Exec Time 35 ms
Memory 3444 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 3 ms 512 KiB
02.txt AC 3 ms 512 KiB
03.txt AC 3 ms 512 KiB
04.txt AC 3 ms 512 KiB
05.txt AC 3 ms 512 KiB
06.txt AC 34 ms 3124 KiB
07.txt AC 33 ms 3092 KiB
08.txt AC 32 ms 3444 KiB
09.txt AC 30 ms 3188 KiB
10.txt AC 34 ms 3068 KiB
11.txt AC 34 ms 3444 KiB
12.txt AC 33 ms 3124 KiB
13.txt AC 30 ms 3188 KiB
14.txt AC 32 ms 3056 KiB
15.txt AC 32 ms 3056 KiB
16.txt AC 26 ms 2548 KiB
17.txt AC 35 ms 3000 KiB
18.txt AC 33 ms 3060 KiB
19.txt AC 33 ms 3060 KiB
20.txt AC 34 ms 3060 KiB
21.txt AC 34 ms 3060 KiB
22.txt AC 29 ms 3188 KiB
23.txt AC 30 ms 3188 KiB
24.txt AC 1 ms 256 KiB
s1.txt AC 1 ms 256 KiB
s2.txt AC 1 ms 256 KiB