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

using namespace std;

int main() {
	vector<int> minlen(2020);
	vector<vector<int>> lens;

	std::ios_base::sync_with_stdio(false);

	int levels;
	cin >> levels;
	int path;
	cin >> path;
	int year;
	int depth;
	for (int i = 0; i < 2020; i++) {
		minlen[i] = 1000000000;
	}
	for (int i = 0; i < levels; i++) {
		vector<int> level;
		lens.push_back(level);
		// handle first one (j = 0)
		cin >> year;
		if (i == 0) {
			lens[i].push_back(1);
			minlen[year] = 1;
//			cout << "1" << endl;
			continue;
		} else {
			depth = i + 1;
			lens[i].push_back(depth);
//			cout << depth << " ";
			if (minlen[year] > depth) {
				minlen[year] = depth;
			}
		}
		// handle internals
		for (int j = 1; j < i; j++) {
			cin >> year;
			int width = min(j + 1, i - j + 1);
//			cout << i << " " << j << "width: " << width << endl;
			depth = width * (i - width + 2);
//			cout << depth << " ";
			lens[i].push_back(depth);
			if (minlen[year] > depth) {
				minlen[year] = depth;
			}
		}
		// handle last one (j = i)
		cin >> year;
		depth = i + 1;
//		cout << depth << endl;
		lens[i].push_back(depth);
		if (minlen[year] > depth) {
			minlen[year] = depth;
		}
			
	}
	for (int i = 0; i < 2020; i++) {
//		if (minlen[i] < 1000000000) {
//			cout << i << ": " << minlen[i] << endl;
//		}
		if (minlen[i] <= path) {
			cout << i << endl;
			break;
		}
	}
	return 0;
}