#include <bits/stdc++.h> #define ll long long const int MAX_N = 300; const int MAX_K = 1e5; const ll MIN = -1e13; int n; ll a[MAX_N+3]; ll t[MAX_K+3]; int k = 0; bool add_segment(int j) { std::vector<ll> seg; for (int i = 0; i < j; i++) { ll x = a[1]; ll sum = 0; // suma liczb [h, i) for (int h = i-1; h >= 0; h--) { // [h, i] musi byc ok sum += seg[h]; // sum + x <= a[i-h+1] // x <= a[i-h+1] - sum x = std::min(a[i-h+1] - sum, x); } seg.push_back(x); } // sprawdzam czy suma na seg >= a[j] ll sum = 0; for (auto x: seg) sum += x; if (sum < a[j]) return false; seg.back() -= sum - a[j]; if (j > 1) { t[k] = MIN; k++; } for (auto x: seg) { t[k] = x; k++; } return true; } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin >> n; for (int i = 1; i <= n; i++) std::cin >> a[i]; for (int i = 1; i <= n; i++) { if (!add_segment(i)) { std::cout << "NIE\n"; return 0; } } std::cout << "TAK\n" << k << "\n"; for (int i = 0; i < k; i++) std::cout << t[i] << " "; std::cout << "\n"; }
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 | #include <bits/stdc++.h> #define ll long long const int MAX_N = 300; const int MAX_K = 1e5; const ll MIN = -1e13; int n; ll a[MAX_N+3]; ll t[MAX_K+3]; int k = 0; bool add_segment(int j) { std::vector<ll> seg; for (int i = 0; i < j; i++) { ll x = a[1]; ll sum = 0; // suma liczb [h, i) for (int h = i-1; h >= 0; h--) { // [h, i] musi byc ok sum += seg[h]; // sum + x <= a[i-h+1] // x <= a[i-h+1] - sum x = std::min(a[i-h+1] - sum, x); } seg.push_back(x); } // sprawdzam czy suma na seg >= a[j] ll sum = 0; for (auto x: seg) sum += x; if (sum < a[j]) return false; seg.back() -= sum - a[j]; if (j > 1) { t[k] = MIN; k++; } for (auto x: seg) { t[k] = x; k++; } return true; } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin >> n; for (int i = 1; i <= n; i++) std::cin >> a[i]; for (int i = 1; i <= n; i++) { if (!add_segment(i)) { std::cout << "NIE\n"; return 0; } } std::cout << "TAK\n" << k << "\n"; for (int i = 0; i < k; i++) std::cout << t[i] << " "; std::cout << "\n"; } |