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
//Przemysław Jakub Kozłowski
#include <iostream>
#include <cstdio>
#include <algorithm>
#define FI first
#define SE second
#define MP make_pair
using namespace std;
typedef long long LL;
const int N = 100005;

bool cmp(int a, int b);

int n,z;
pair<int,int> tab[N]; // (strata, zysk)
int kol[N];

int main()
{
    scanf("%d%d", &n, &z);
    for(int i = 1;i <= n;i++)
        scanf("%d%d", &tab[i].FI, &tab[i].SE);
    for(int i = 1;i <= n;i++) kol[i] = i;
    sort(kol+1, kol+n+1, cmp);

    LL zycie = z;
    bool wyn = true;
    for(int i = 1;i <= n;i++)
    {
        zycie -= tab[kol[i]].FI;
        if(zycie <= 0)
        {
            wyn = false;
            break;
        }
        zycie += tab[kol[i]].SE;
    }

    if(wyn)
    {
        printf("TAK\n");
        for(int i = 1;i <= n;i++)
            printf("%d ", kol[i]);
        printf("\n");
    }
    else printf("NIE\n");
    return 0;
}

bool cmp(int a, int b)
{
    int adod = 0, bdod = 0;
    if(tab[a].SE > tab[a].FI) adod = 1;
    if(tab[b].SE > tab[b].FI) bdod = 1;

    if(adod != bdod) return adod > bdod;
    else if(adod == 0) return tab[a].SE > tab[b].SE;
    else return tab[a].FI < tab[b].FI;
}