#include <bits/stdc++.h> using namespace std; int main () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> d1, d2, d3; int res = 2019; int x; cin >> x; d1.push_back(1); res = x; if (n == 1 || k == 1) { cout << res << endl; return 0; } cin >> x; res = min(res, x); cin >> x; res = min(res, x); d2.push_back(2); d2.push_back(2); if (n == 2 || k == 2) { cout << res << endl; return 0; } for (int i = 3; i <= n; i++) { cin >> x; d3.push_back(d2[0] + 1); if (d3[0] <= k) { res = min(res, x); } for (int j = 2; j <= i - 1; j++) { cin >> x; d3.push_back(d2[j - 2] + d2[j - 1] - d1[j - 2] + 1); if (d3[j-1] <= k) res = min(res, x); } cin >> x; d3.push_back(d2[i - 2] + 1); if (d3[i - 1] <= k) { res = min(res, x); } else { cout << res << endl; return 0; } swap(d1, d2); swap(d2, d3); d3.clear(); } cout << res << endl; 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <bits/stdc++.h> using namespace std; int main () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int> d1, d2, d3; int res = 2019; int x; cin >> x; d1.push_back(1); res = x; if (n == 1 || k == 1) { cout << res << endl; return 0; } cin >> x; res = min(res, x); cin >> x; res = min(res, x); d2.push_back(2); d2.push_back(2); if (n == 2 || k == 2) { cout << res << endl; return 0; } for (int i = 3; i <= n; i++) { cin >> x; d3.push_back(d2[0] + 1); if (d3[0] <= k) { res = min(res, x); } for (int j = 2; j <= i - 1; j++) { cin >> x; d3.push_back(d2[j - 2] + d2[j - 1] - d1[j - 2] + 1); if (d3[j-1] <= k) res = min(res, x); } cin >> x; d3.push_back(d2[i - 2] + 1); if (d3[i - 1] <= k) { res = min(res, x); } else { cout << res << endl; return 0; } swap(d1, d2); swap(d2, d3); d3.clear(); } cout << res << endl; return 0; } |