A - Amr and Music(507A - Amr and Music)

 

  1. https://codeforces.com/problemset/problem/507/A

  2. Examples
    input
    Copy
    4 10
    4 3 1 2
    output
    Copy
    4
    1 2 3 4
    input
    Copy
    5 6
    4 3 1 1 2
    output
    Copy
    3
    1 3 4
    input
    Copy
    1 3
    4
    output
    Copy
    0
    Note

    In the first test Amr can learn all 4 instruments.

    In the second test other possible solutions are: {2, 3, 5} or {3, 4, 5}.

    In the third test Amr doesn't have enough time to learn the only presented instrument.


  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. using namespace std;
  7. int n, k, sum, num, b, index[101];
  8. vector < pair<int, int>> arr;
  9. int main() {
  10. cin >> n >> k;
  11. for (int i = 1; i <= n; i++) {
  12. cin >> b;
  13. arr.push_back( make_pair(b, i));
  14. }
  15. sort(arr.begin(), arr.end());
  16. for (int i = 0; i < n; i++) {
  17. sum += arr[i].first;
  18. if (sum <= k) {
  19. num++;
  20. index[i] = arr[i].second;
  21. } else {
  22. break;
  23. }
  24. }
  25. cout << num << endl;
  26. for (int i = 0; i < num; i++) {
  27. cout << index[i] << " ";
  28. }
  29. }

Comments

Popular posts from this blog

Codeforce Problem 1703A. YES or YES?

Aptitude test assistant programmer 2018

1041A. Heist solution