#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int main () {
int i, n, k, z, dodatnia;
queue < int > wyniki;
pair < pair < int, int > , pair < int, int > > tab[100000];
scanf("%d%d", &n, &z);
dodatnia = 0;
for(i = 0; i < n; i++) {
scanf("%d%d", &tab[i].first.second, &tab[i].second.first);
tab[i].second.second = i+1;
tab[i].first.first = tab[i].second.first - tab[i].first.second;
if(tab[i].first.first >= 0)
dodatnia++;
}
sort(tab, tab+n);
int a = n-dodatnia;
bool p = 0;
for(i = n-1; i > a; i--) {
swap(tab[i].first.first, tab[i].first.second);
}
sort(tab+a, tab+n);
for(i = a; i < n; i++) {
z = z - tab[i].first.first;
if( z <= 0 ) {
printf("NIE");
p = 1;
break;
}
z = z + tab[i].second.first;
wyniki.push(tab[i].second.second);
}
sort(tab, tab+a);
for(i = 0 ; i <a; i++) {
z = z + tab[i].first.first;
if( z <= 0 ) {
printf("NIE");
p = 1;
break;
}
wyniki.push(tab[i].second.second);
}
if(p == 0) {
printf("TAK\n");
while(wyniki.empty() == false) {
printf("%d ", wyniki.front());
wyniki.pop();
}
}
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 | #include <cstdio> #include <queue> #include <algorithm> using namespace std; int main () { int i, n, k, z, dodatnia; queue < int > wyniki; pair < pair < int, int > , pair < int, int > > tab[100000]; scanf("%d%d", &n, &z); dodatnia = 0; for(i = 0; i < n; i++) { scanf("%d%d", &tab[i].first.second, &tab[i].second.first); tab[i].second.second = i+1; tab[i].first.first = tab[i].second.first - tab[i].first.second; if(tab[i].first.first >= 0) dodatnia++; } sort(tab, tab+n); int a = n-dodatnia; bool p = 0; for(i = n-1; i > a; i--) { swap(tab[i].first.first, tab[i].first.second); } sort(tab+a, tab+n); for(i = a; i < n; i++) { z = z - tab[i].first.first; if( z <= 0 ) { printf("NIE"); p = 1; break; } z = z + tab[i].second.first; wyniki.push(tab[i].second.second); } sort(tab, tab+a); for(i = 0 ; i <a; i++) { z = z + tab[i].first.first; if( z <= 0 ) { printf("NIE"); p = 1; break; } wyniki.push(tab[i].second.second); } if(p == 0) { printf("TAK\n"); while(wyniki.empty() == false) { printf("%d ", wyniki.front()); wyniki.pop(); } } return 0; } |
English