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
#include<bits/stdc++.h>

using namespace std;

vector<int> winesNeededToTake;
vector< pair<int, int> > wines;

void addRow(int index);

void initWinesNeededToTake(int n){
	for(int i=0; i<n; i++){
		addRow(i);
	}
}

int triangleNumber(int n){
	return n*(n+1)/2;
}

void addRow(int index){
	if(index == 0){
		winesNeededToTake.push_back(1);
	}
	else if(index == 1) {
		winesNeededToTake.push_back(2);
		winesNeededToTake.push_back(2);
	}
	else{
		int lastRowIndex = triangleNumber(index-1);
		auto preLastRowIndex = triangleNumber(index-2);

		winesNeededToTake.push_back(index+1);
		for(int i=1; i<index; i++){
			winesNeededToTake.push_back(winesNeededToTake[lastRowIndex + i - 1] 
								+ winesNeededToTake[lastRowIndex + i] 
								- winesNeededToTake[preLastRowIndex + i - 1] + 1); // add parents and next cut the same part from their same parent
		}
		winesNeededToTake.push_back(index+1);
	}
}

bool comparator ( const pair<int,int>& l, const pair<int,int>& r){ 
	return l.second < r.second;
}


int main(){
	int n, k;
	cin >> n >> k;

	initWinesNeededToTake(n);
	
	for(int i=0; i<triangleNumber(n); i++){
		int w; cin >> w;
		wines.push_back(make_pair(w, winesNeededToTake[i]));
	}

	sort(wines.begin(), wines.end(), comparator);

	int answer=2020;

	for(int i=0; i<triangleNumber(n) && wines[i].second <= k; i++){
		if(wines[i].first < answer) answer = wines[i].first;
	}
	cout << answer <<"\n";
}