#include <bits/stdc++.h> using namespace std; int get_bottle_count(int i, int j, const vector<int> &vintages) { int count = 1; if (j - 1 > 0) { count += vintages[j - 1]; } if (j < i) { count += vintages[j]; } return count; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; int min_vintage = 2020; int curr; vector<int> prev_row(n); prev_row[0] = 0; vector<int> curr_row(n); for (int i = 0; i < n; i++) { for (int j = 0; j < i + 1; j++) { cin >> curr; int curr_bottles = get_bottle_count(i, j, prev_row); curr_row[j] = curr_bottles; if (curr < min_vintage && curr_bottles <= k) { min_vintage = curr; } } prev_row = curr_row; } cout << min_vintage; 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 | #include <bits/stdc++.h> using namespace std; int get_bottle_count(int i, int j, const vector<int> &vintages) { int count = 1; if (j - 1 > 0) { count += vintages[j - 1]; } if (j < i) { count += vintages[j]; } return count; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; int min_vintage = 2020; int curr; vector<int> prev_row(n); prev_row[0] = 0; vector<int> curr_row(n); for (int i = 0; i < n; i++) { for (int j = 0; j < i + 1; j++) { cin >> curr; int curr_bottles = get_bottle_count(i, j, prev_row); curr_row[j] = curr_bottles; if (curr < min_vintage && curr_bottles <= k) { min_vintage = curr; } } prev_row = curr_row; } cout << min_vintage; return 0; } |