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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Runda1_B.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

//#include "pch.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>

int iNeededBootle = 0;
int iRow = 0;
std::vector<int> wineVintage;
std::vector<int> minIndexes;
//std::vector<int> cutWineVintage;
int maxIndex = 0;

template <typename T>
std::vector<size_t> sort_indexes(const std::vector<T> &v) {

	// initialize original index locations
	std::vector<size_t> idx(v.size());
	std::iota(idx.begin(), idx.end(), 0);

	// sort indexes based on comparing values in v
	sort(idx.begin(), idx.end(),
		[&v](size_t i1, size_t i2) {return v[i1] < v[i2]; });

	return idx;
}

int computeMaxIndex()
{
	int index = (iNeededBootle * (iNeededBootle + 1) / 2) - 1;
	if (index > wineVintage.size())
		index = wineVintage.size() - 1;
	return index;
}

void cutVector()
{
	int sizeOfVector = wineVintage.size();
	if (maxIndex + 1 != wineVintage.size())
		wineVintage.erase(wineVintage.begin() + maxIndex + 1, wineVintage.end());
}

int computeRow(int oldestIndex)
{
	double sqrtDelta = sqrt(1 + 8 * oldestIndex);
	double dRow = (-1 + sqrtDelta) / 2;
	int row = ceil(dRow);
	return row;
}

int findOldestWine(int oldestIndex)
{
	int posInRow = 0;
	int addValue = 2;
	int site = 0;
	int row = computeRow(oldestIndex);
	int maxRange = row * (row + 1) / 2;
	int minRange = (row - 1)*row / 2;
	if (oldestIndex != 0)
	posInRow = (oldestIndex - minRange) % row;
	if (posInRow >= row / 2)
		site = 1; //rigth side
	else
		site = 0; //left side
	if (posInRow == 0 || posInRow == row-1)
		return 1;

	int step = 1; // take oldest wine
	int startRow = row;
	while (row > 1 && posInRow % startRow != 0 && (posInRow+1) % startRow !=0)
	{
		row--;
		if (site == 1)
			posInRow++;
		else posInRow--;
		if (step == 1 && row >= 2)
		{
			step += 2;
		}
		else
		{
			addValue = startRow - row + 1; // 2 in first step
			step += addValue;
		}
	}
	if (( posInRow % startRow == 0 || posInRow+1 == startRow ) && row > 1)
	{
		while (row > 1)
		{
			if (addValue < row)
				step += addValue;
			else
				step += row;
			row--;
		}
	}
	//step++;
	if (step <= iNeededBootle)
		return 1;
	else
		return 0;
}



int main()
{
		scanf("%d%d", &iRow, &iNeededBootle);

		for (int i = 0; i < iRow; i++)
		{
			unsigned k = 0;
			while (k <= i)
			{
				int vintage = 0;
				scanf("%d", &vintage);
				wineVintage.push_back(vintage);
				k++;
			}
		}
		maxIndex = computeMaxIndex();
		cutVector();
		for (auto i : sort_indexes(wineVintage)) {
			minIndexes.push_back(i);
		}

		int result = 0;
		int k = 0;
		while (result == 0)
		{
			int oldestIndex = minIndexes[k];
			result = findOldestWine(oldestIndex);
			k++;
		}
		printf("%d", wineVintage[minIndexes[--k]]);

}