We are asked to choose at most subproject with max total value. Note that he cannot choose any subproject mutiple times.
The solution is to choose top subprojects with greatest value.
We can show that this greedy solution is not worse than any other solution by a simple Exchange Argument.
Let be the set of the highest-value subprojects, and let be any optimal solution. If O \neq G, then there is some g \in G \setminus O and some o \in O \setminus G. Since is among the top values, value(g) \ge value(o). Replacing by does not decrease the total value. Repeating this exchange, we transform into without decreasing value. Hence is optimal.
#include <bits/stdc++.h>
using namespace std;
signed main() {
int T=1;
cin >> T;
for (int tt = 0; tt < T; tt += 1) {
int t;
cin >> t;
int n;
cin >> n;
vector<int> a(n);
for (auto &x : a) {
cin >> x;
}
auto it = min(a.end(), a.begin()+t);
nth_element(a.begin(), it, a.end(), greater<int>());
cout << accumulate(a.begin(), it, 0ll) << "\n";
}
}Time complexity: for finding kth order statistic or \mathcal{O}(p \cdot log p) for sorting.