Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n;
- cin >> n;
- vector<int> a(n);
- for(int &i : a)
- cin >> i;
- sort(a.begin(), a.end());
- vector<int> dp(n);
- dp[1] = a[1] - a[0];
- if (n > 2)
- dp[2] = a[2] - a[0];
- vector<int> path(n, -1);
- for (int i = 3; i < n; i++) {
- if (dp[i - 1] < dp[i - 2]) {
- dp[i] = dp[i - 1] + a[i] - a[i - 1];
- path[i] = i - 1;
- } else {
- dp[i] = dp[i - 2] + a[i] - a[i - 1];
- path[i] = i - 2;
- }
- }
- vector<int> answer;
- int i = n - 1;
- while (path[i] != -1) {
- answer.push_back(i);
- i = path[i];
- }
- reverse(answer.begin(), answer.end());
- for (int i : answer)
- cout << i << " ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement