#include <iostream>
#include <cstdio>
#include <queue>
#define ff first
#define ss second
using namespace std;
int n, z, hp, d, a;
priority_queue <pair <pair <int,int>, int> >Q_up, Q_down;
vector <int> odp, odp2;
int main()
{
scanf("%d%d", &n, &z);
hp=z;
for(int i=1;i<=n;i++)
{
scanf("%d%d", &d, &a);
hp+=(a-d);
if(d<=a)
Q_up.push(make_pair(make_pair(d*-1, a), i));
else
Q_down.push(make_pair(make_pair(a*-1, d), i));
}
if(hp<=0)
{
printf("NIE\n");
return 0;
}
while(!Q_up.empty())
{
if(Q_up.top().ff.ff*-1>=z)
{
printf("NIE\n");
return 0;
}
d=Q_up.top().ff.ff*-1;
a=Q_up.top().ff.ss;
z+=(a-d);
odp.push_back(Q_up.top().ss);
Q_up.pop();
}
while(!Q_down.empty())
{
if(Q_down.top().ff.ff*-1>=hp)
{
printf("NIE\n");
return 0;
}
d=Q_down.top().ff.ff*-1;
a=Q_down.top().ff.ss;
hp+=(a-d);
odp2.push_back(Q_down.top().ss);
Q_down.pop();
}
printf("TAK\n");
for(int i=0;i<odp.size();i++)
{
printf("%d ", odp[i]);
}
for(int i=odp2.size()-1;i>=0;i--)
{
printf("%d ", odp2[i]);
}
return 0;
}
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 | #include <iostream> #include <cstdio> #include <queue> #define ff first #define ss second using namespace std; int n, z, hp, d, a; priority_queue <pair <pair <int,int>, int> >Q_up, Q_down; vector <int> odp, odp2; int main() { scanf("%d%d", &n, &z); hp=z; for(int i=1;i<=n;i++) { scanf("%d%d", &d, &a); hp+=(a-d); if(d<=a) Q_up.push(make_pair(make_pair(d*-1, a), i)); else Q_down.push(make_pair(make_pair(a*-1, d), i)); } if(hp<=0) { printf("NIE\n"); return 0; } while(!Q_up.empty()) { if(Q_up.top().ff.ff*-1>=z) { printf("NIE\n"); return 0; } d=Q_up.top().ff.ff*-1; a=Q_up.top().ff.ss; z+=(a-d); odp.push_back(Q_up.top().ss); Q_up.pop(); } while(!Q_down.empty()) { if(Q_down.top().ff.ff*-1>=hp) { printf("NIE\n"); return 0; } d=Q_down.top().ff.ff*-1; a=Q_down.top().ff.ss; hp+=(a-d); odp2.push_back(Q_down.top().ss); Q_down.pop(); } printf("TAK\n"); for(int i=0;i<odp.size();i++) { printf("%d ", odp[i]); } for(int i=odp2.size()-1;i>=0;i--) { printf("%d ", odp2[i]); } return 0; } |
English