main.cpp 862 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. struct Box {
  5. int Ai;
  6. int Bi;
  7. };
  8. bool compare(Box a, Box b) {
  9. return a.Ai-a.Bi < b.Ai - b.Bi;
  10. }
  11. bool judge(int V, int N, Box *boxes) {
  12. sort(boxes, boxes + N, compare);
  13. for (int i = 0; i < N; i++) {
  14. if (boxes[i].Bi > V || boxes[i].Ai > V) {
  15. return false;
  16. }
  17. V -= boxes[i].Ai;
  18. }
  19. return true;
  20. }
  21. int main() {
  22. int T, V, N;
  23. Box *boxes;
  24. while (cin >> T) {
  25. for (int i = 0; i < T; i++) {
  26. cin >> V >> N;
  27. boxes = new Box[N];
  28. for (int j = 0; j < N; j++) {
  29. Box box;
  30. cin >> box.Ai >> box.Bi;
  31. boxes[j] = box;
  32. }
  33. cout << (judge(V, N, boxes) ? "Yes" : "No") << endl;
  34. free(boxes);
  35. }
  36. }
  37. return 0;
  38. }