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

int main(int argc, char* argv[])
{
	unsigned int potwory(0);
	unsigned int punkty(0);
	scanf( "%d", &potwory );
	scanf( "%d", &punkty );

	int **tab = new int*[potwory];
	bool *flag = new bool[potwory];
	unsigned int *seq = new unsigned int[potwory];
	for (unsigned int i=0; i<potwory; i++)
	{
		tab[i] = new int[2];
		for (unsigned int j=0; j<2; j++)
		{
			scanf( "%d", &tab[i][j] );
		}
	}

	unsigned int index = 0;
	int waga = 0;
	int k = 0;

	for (unsigned int i=0; i<potwory; i++)
	{
		index = 0;
		while ( (index<potwory-1) && (!flag[index] || punkty < tab[index][0]) )
		{
			index++;
		}

		if (index >= potwory)
		{
			printf("NIE\n");
			break;
		}

		waga = tab[index][0]-tab[index][1];
		for (unsigned int j=index+1; j<potwory; j++)
		{
			if ( flag[j] && (tab[j][0]<=punkty) /*&& (tab[j][0]-tab[j][1] < waga)*/ )
			{
				index=j;
				waga=tab[j][0]-tab[j][1];
			}
		}

		punkty += tab[index][1]-tab[index][0];
		flag[index]=false;
		seq[k]=index;
		k++;
	}

	if ( punkty > 0 )
	{
		printf("TAK\n");
		for (unsigned int i=0; i<potwory; i++)
			printf("%d ", seq[i]+1);
	}

	delete [] flag;
	delete [] seq;
	for (unsigned int i=0; i<potwory; i++)
	{
		delete [] tab[i];
	}
	delete [] tab;
	return 0;
}