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

#define MAX_PILE_HEIGHT	2000
#define LOWER_INDEX(level) (level*(level-1)/2)
#define UPPER_INDEX(level) ((LOWER_INDEX(level))+level-1)



static int capacity(int level);
static int botlesDug(int maxlevel, int layers);
static int findMaxDugLevel(int initialLevel, int layers, int maxBotlesDug);


int n;	/* ilość poziomów */
int k;	/* ilość butelek do pobrania */
int botles;	/* ilość butelek w stosie */
unsigned int result = 2020;	/* najstarsza butla z pobranych */

unsigned int pile[MAX_PILE_HEIGHT*(MAX_PILE_HEIGHT+1)/2];


int main()
{
	(void)scanf("%d%d", &n, &k);
	botles = capacity(n);

	for(int i=0; i<botles; i++)
	{
		(void)scanf("%d", &pile[i]);
	}

	int half = n/2 + n%2;
	int lvl;
	int lowerind, upperind;
	for(int i=1; i<=half; i++)
	{
		lvl = findMaxDugLevel(n, i, k);
		while( lvl && ((lowerind=(LOWER_INDEX(lvl)+i-1)) <= (upperind=UPPER_INDEX(lvl)-i+1)) )
		{
			result = result>pile[lowerind] ? pile[lowerind] : result;
			result = result>pile[upperind] ? pile[upperind] : result;
			lvl--;
		}

	}

	printf("%d\n", result);

	return 0;
}

/****************************************/

static int capacity(int level)
{
	return (level * (level+1) / 2);
}

static int botlesDug(int maxlevel, int layers)
{
	return (capacity(maxlevel) - capacity(maxlevel-layers) - capacity(layers-1));
}

static int findMaxDugLevel(int initialLevel, int layers, int maxBotlesDug)
{
	int botles;

	do {
		botles = botlesDug(initialLevel--,layers);
	} while(botles>maxBotlesDug);

	return ++initialLevel;
}