1
  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
103
104
105
106
107
108
#include <iostream>
#include <vector>

using namespace std;

typedef unsigned long UINT;
typedef signed long SINT;

/** Redirect stdin to provide input with Qt Creator */
void reopenInput(int argc, char** argv) {
#ifdef LOCAL
	if (argc > 1) {
		auto inFile = argv[1];
		cerr << "Reopening stdin to: " << inFile << endl;
		auto success = freopen(inFile, "r", stdin);
		if (!success) {
			cerr << "Error when opening file!" << endl;
			cerr << std::strerror(errno) << endl;
		}
	}
#endif
}

static vector<SINT> streaks, scores;
static UINT n;

void processInput()
{
	cin >> n;
	cerr << "N: " << n << endl;
	streaks.reserve(n);
	scores.reserve(n);

	cerr << "Sequences:" << endl;

	for (UINT i = 0; i < n; i++) {
		SINT tmp;
		cin >> tmp;
		streaks.push_back(tmp);
		cerr << "Streak " << i+1 << ":\t" << streaks[i] << endl;
	}
}

[[noreturn]]
void fail()
{
	cout << "NIE" << endl;
	exit(0);
}

void validateScores()
{
	UINT curr_party_idx = scores.size() - 1;
	auto last_score = scores[curr_party_idx];
#if LOCAL
	cerr << "debug last score " << last_score
		 << " of party " << curr_party_idx + 1
		 << endl;
#endif

	SINT streak_sum = 0;
	for (UINT streak = 1; streak <= scores.size(); streak ++) {
		streak_sum += scores[curr_party_idx - streak + 1];
		auto streak_allowed = streaks[streak - 1];
#if LOCAL
		cerr << "debug substreak " << streak
			 << " has score " << streak_sum
			 << " whereas allowed is " << streak_allowed
			 << endl;
#endif

		if (streak_sum > streak_allowed) fail();
	}
}

void success()
{
	cout << "TAK" << endl;

	cout << scores.size() << endl;

	for (UINT i = 0; i < scores.size(); i++) {
		cout << scores[i] << " ";
	}
	cout << endl;
}

int main(int argc, char** argv)
{
	reopenInput(argc, argv);
	processInput();

	SINT sofar_score = 0;

	for (UINT i = 0; i < n; i++) {
		auto currstreak_score = streaks[i];

		auto last_score = currstreak_score - sofar_score;
		sofar_score = currstreak_score;

		cerr << "Party " << i+1 << " score: " << last_score << endl;
		scores.push_back(last_score);
		validateScores();
	}

	success();
	return 0;
}