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
#include <stdio.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

bool visited[2000007];
pair <int, pair <int, int> > barrels[2000007];
vector < pair <int, int> > values;
queue <int> qu;
int n, k;

int main()
{
	scanf("%d", &n);
	scanf("%d", &k);

	int level = 1, barrelsNumberInLevel = 1, currentBarrelInLevel = 0;
	for (int i = 0; i < n*(n + 1) / 2; i++) {
		int temp;
		scanf("%d", &temp);
		barrels[i].first = temp;

		values.push_back(make_pair(temp, i));

		if (currentBarrelInLevel == 0) barrels[i].second.first = -1;
		else barrels[i].second.first = (((level - 1)*(level - 2)) / 2) + currentBarrelInLevel - 1;

		if (currentBarrelInLevel == barrelsNumberInLevel - 1) barrels[i].second.second = -1;
		else barrels[i].second.second = (((level - 1)*(level - 2)) / 2) + currentBarrelInLevel;

		if (i == (level) * (level + 1) / 2 - 1) {
			level++;
			currentBarrelInLevel = 0;
			barrelsNumberInLevel++;
		}
		else currentBarrelInLevel++;
	}
	sort(values.begin(), values.end());

	for (int i = 0; ; i++) {
		qu.push(values[i].second);
		int takenBarrels = 0;

		while (!qu.empty()) {
			int currentElement = qu.front();
			qu.pop();
			if (currentElement == -1 || visited[currentElement] == 1) continue;
			visited[currentElement] = 1;

			qu.push(barrels[currentElement].second.first);
			qu.push(barrels[currentElement].second.second);

			takenBarrels++;
			if (currentElement == 0) {
				printf("%d", barrels[values[i].second].first);
				return 0;
			}
			if (takenBarrels == k) break;
		}
	}

}