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

using namespace std;

struct bottle
{
    int depth;
    int value;
};

int main()
{
    int maximum = 2020;
    vector < vector <bottle> > bottles;
    int h, n, temp;
    cin >> h >> n;
    for(int i = 0; i < h; i++)
    {
        bottles.push_back(vector <bottle>());
        for(int j = 0; j < i + 1; j++)
        {
            cin >> temp;
            bottles[i].push_back(bottle {0, temp});
        }
    }
    bottles[0][0].depth = 1;
    bottles[1][0].depth = 2;
    bottles[1][1].depth = 2;
    for(int i = 3; i <= h; i++)
    {
        bottles[i - 1][0].depth = i;
        bottles[i - 1][i - 1].depth = i;
        int k = i - 2;
        for(int j = 1; j < (i + 1) / 2; j++)
        {
            bottles[i - 1][j].depth = bottles[i - 1][j - 1].depth + k;
            bottles[i - 1][i - j - 1].depth = bottles[i - 1][i - j].depth + k;
            k -=2;
        }
    }
    for(int i = 0; i < h; i++)
    {
        for(int j = 0; j < i + 1; j++)
        {
            if(bottles[i][j].depth <= n && bottles[i][j].value < maximum)
            {
                maximum = bottles[i][j].value;
            }
        }
    }
    cout << maximum << endl;
    return 0;
}