#include <cstdio> #include <vector> #include <algorithm> using i64 = long long; const i64 inf = 1e12; int main() { int n; scanf("%d", &n); std::vector<i64> dp(n + 1, inf), a(n); for (int i = 0; i < n; ++i) scanf("%lld", &a[i]); dp[0] = 0; for (int i = 0; i < n; ++i) { for (int d = 1; i + d <= n; ++d) { dp[i + d] = std::min(dp[i + d], dp[i] + a[d - 1]); } } std::vector<i64> ret; bool valid = true; for (int d = 1; d <= n; ++d) { if (dp[d] - a[d - 1] < 0) valid = false; for (int i = 1; i <= d; ++i) { ret.push_back(dp[i] - dp[i - 1]); } ret.push_back(-inf); } if (!valid) puts("NIE"); else { puts("TAK"); printf("%d\n", (int)ret.size()); for (auto &x: ret) printf("%lld ", x); puts(""); } return 0; }
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 | #include <cstdio> #include <vector> #include <algorithm> using i64 = long long; const i64 inf = 1e12; int main() { int n; scanf("%d", &n); std::vector<i64> dp(n + 1, inf), a(n); for (int i = 0; i < n; ++i) scanf("%lld", &a[i]); dp[0] = 0; for (int i = 0; i < n; ++i) { for (int d = 1; i + d <= n; ++d) { dp[i + d] = std::min(dp[i + d], dp[i] + a[d - 1]); } } std::vector<i64> ret; bool valid = true; for (int d = 1; d <= n; ++d) { if (dp[d] - a[d - 1] < 0) valid = false; for (int i = 1; i <= d; ++i) { ret.push_back(dp[i] - dp[i - 1]); } ret.push_back(-inf); } if (!valid) puts("NIE"); else { puts("TAK"); printf("%d\n", (int)ret.size()); for (auto &x: ret) printf("%lld ", x); puts(""); } return 0; } |