Advertisement
Georgiy1108

Untitled

Jul 1st, 2020
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int n;
  8.     cin >> n;
  9.     vector<int> a(n);
  10.     for(int &i : a)
  11.         cin >> i;
  12.     sort(a.begin(), a.end());
  13.     vector<int> dp(n);
  14.     dp[1] = a[1] - a[0];
  15.     if (n > 2)
  16.         dp[2] = a[2] - a[0];
  17.     vector<int> path(n, -1);
  18.     for (int i = 3; i < n; i++) {
  19.         if (dp[i - 1] < dp[i - 2]) {
  20.             dp[i] = dp[i - 1] + a[i] - a[i - 1];
  21.             path[i] = i - 1;
  22.         } else {
  23.             dp[i] = dp[i - 2] + a[i] - a[i - 1];
  24.             path[i] = i - 2;
  25.         }
  26.     }
  27.     vector<int> answer;
  28.     int i = n - 1;
  29.     while (path[i] != -1) {
  30.         answer.push_back(i);
  31.         i = path[i];
  32.     }
  33.     reverse(answer.begin(), answer.end());
  34.     for (int i : answer)
  35.         cout << i << " ";
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement