#include <bits/stdc++.h> typedef unsigned short us; typedef short s; typedef unsigned long ul; typedef long l; typedef unsigned long long ull; typedef long long ll; typedef long double ld; const l INF = 1e9 + 9; const l MOD = 1e9 + 7; const l PRIME = 200003; const ll L_INF = 1e18 + 1; const double EPS = 10e-9; #define FOR(x, y, z) for (l z = x; z < y; z++) #define FORE(x, y, z) for (l z = x; z <= y; z++) #define FORD(x, y, z) for (l z = x; z > y; z--) #define FORDE(x, y, z) for (l z = x; z >= y; z--) #define REP(x, y) for (l y = 0; y < x; y++) #define PF push_front #define POF pop_front #define PB push_back #define POB pop_back #define MP make_pair #define FS first #define SC second using namespace std; l height, num_moves, res = INF; l prefix_sum[2001005]; vector<l> bottles[2015]; fstream input, output; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> height >> num_moves; FORE(1, height, i) { FORE(1, i, j) { l a; cin >> a; bottles[i].PB(a); } } prefix_sum[1] = 1; prefix_sum[2] = 2; prefix_sum[3] = 2; FORE(3, height, i) { FORE(1, i, j) { l pos = (((i - 1) * i) / 2) + j; if (j == 1) prefix_sum[pos] = prefix_sum[(((i - 2) * (i - 1)) / 2) + 1] + 1; else if (j == i) prefix_sum[pos] = prefix_sum[(((i - 1) * i) / 2)] + 1; else { prefix_sum[pos] = prefix_sum[((((i - 2) * (i - 1)) / 2) + j - 1)] + prefix_sum[((((i - 2) * (i - 1)) / 2) + j)] - prefix_sum[((((i - 3) * (i - 2)) / 2) + j - 1)] + 1; } } } FORE(1, height, i) { FORE(1, i, j) { l pos = (((i - 1) * i) / 2) + j; if (prefix_sum[pos] <= num_moves) res = min(res, bottles[i][j - 1]); } } cout << res << "\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 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 | #include <bits/stdc++.h> typedef unsigned short us; typedef short s; typedef unsigned long ul; typedef long l; typedef unsigned long long ull; typedef long long ll; typedef long double ld; const l INF = 1e9 + 9; const l MOD = 1e9 + 7; const l PRIME = 200003; const ll L_INF = 1e18 + 1; const double EPS = 10e-9; #define FOR(x, y, z) for (l z = x; z < y; z++) #define FORE(x, y, z) for (l z = x; z <= y; z++) #define FORD(x, y, z) for (l z = x; z > y; z--) #define FORDE(x, y, z) for (l z = x; z >= y; z--) #define REP(x, y) for (l y = 0; y < x; y++) #define PF push_front #define POF pop_front #define PB push_back #define POB pop_back #define MP make_pair #define FS first #define SC second using namespace std; l height, num_moves, res = INF; l prefix_sum[2001005]; vector<l> bottles[2015]; fstream input, output; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> height >> num_moves; FORE(1, height, i) { FORE(1, i, j) { l a; cin >> a; bottles[i].PB(a); } } prefix_sum[1] = 1; prefix_sum[2] = 2; prefix_sum[3] = 2; FORE(3, height, i) { FORE(1, i, j) { l pos = (((i - 1) * i) / 2) + j; if (j == 1) prefix_sum[pos] = prefix_sum[(((i - 2) * (i - 1)) / 2) + 1] + 1; else if (j == i) prefix_sum[pos] = prefix_sum[(((i - 1) * i) / 2)] + 1; else { prefix_sum[pos] = prefix_sum[((((i - 2) * (i - 1)) / 2) + j - 1)] + prefix_sum[((((i - 2) * (i - 1)) / 2) + j)] - prefix_sum[((((i - 3) * (i - 2)) / 2) + j - 1)] + 1; } } } FORE(1, height, i) { FORE(1, i, j) { l pos = (((i - 1) * i) / 2) + j; if (prefix_sum[pos] <= num_moves) res = min(res, bottles[i][j - 1]); } } cout << res << "\n"; } |