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
#include <iostream>
#include <vector>

int main()
{
	int n, k, amountOfWines = 0;
	int theOldestWine=0;

	std::vector< std::vector<int>> wines;
	std::vector<int> winesOut;

	std::cin >> n >> k;

	wines.resize(n, std::vector<int>(n));

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			std::cin >> wines[i][j];
		}//create a 2d array for wines 
	}

	for (int i = 0; i < k; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			if (amountOfWines == k) break;

			winesOut.push_back(wines[i][j]);
			amountOfWines++;
		}//pick wines from top
	}
	
	for (int i = 0; i < winesOut.size()-1; i++)
	{
		if (winesOut[i] > winesOut[i + 1])
			theOldestWine = winesOut[i + 1];
	}

	std::cout <<'\n'<< theOldestWine << '\n';

	/*for (int i = 0; i < n; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			std::cout << wines[i][j]<<' ';
		}//printing that list
		std::cout << '\n';
	}*///print the wines

	return 0;
}