#include<iostream> #include<cstdio> #include<algorithm> #include<vector> using namespace std; long long int n, z, a, b; vector<int>v; vector<pair<long long int, pair<long long int, int> > >mal, ros; int main() { scanf("%lld %lld", &n, &z); for(int p=1; p<=n; p++) { scanf("%lld %lld", &a, &b); if(b>=a)ros.push_back(make_pair(a, make_pair(b, p))); else mal.push_back(make_pair(b, make_pair(a, p))); } sort(ros.begin(), ros.end()); sort(mal.begin(), mal.end()); for(int p=0; p<ros.size(); p++) { if(ros[p].first>z)break; v.push_back(ros[p].second.second); z-=ros[p].first; z+=ros[p].second.first; } if(v.size()!=ros.size())printf("NIE\n"); else { for(int p=mal.size()-1; p>=0; p--) { if(mal[p].second.first>z)break; v.push_back(mal[p].second.second); z-=mal[p].second.first; z+=mal[p].first; } if(v.size()==n) { printf("TAK\n"); for(int p=0; p<v.size(); p++) { printf("%d ", v[p]); } printf("\n"); } else printf("NIE\n"); } 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 | #include<iostream> #include<cstdio> #include<algorithm> #include<vector> using namespace std; long long int n, z, a, b; vector<int>v; vector<pair<long long int, pair<long long int, int> > >mal, ros; int main() { scanf("%lld %lld", &n, &z); for(int p=1; p<=n; p++) { scanf("%lld %lld", &a, &b); if(b>=a)ros.push_back(make_pair(a, make_pair(b, p))); else mal.push_back(make_pair(b, make_pair(a, p))); } sort(ros.begin(), ros.end()); sort(mal.begin(), mal.end()); for(int p=0; p<ros.size(); p++) { if(ros[p].first>z)break; v.push_back(ros[p].second.second); z-=ros[p].first; z+=ros[p].second.first; } if(v.size()!=ros.size())printf("NIE\n"); else { for(int p=mal.size()-1; p>=0; p--) { if(mal[p].second.first>z)break; v.push_back(mal[p].second.second); z-=mal[p].second.first; z+=mal[p].first; } if(v.size()==n) { printf("TAK\n"); for(int p=0; p<v.size(); p++) { printf("%d ", v[p]); } printf("\n"); } else printf("NIE\n"); } return 0; } |