Advertisement
Josif_tepe

Untitled

Oct 24th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef long long ll;
  7.  
  8. int main()
  9. {
  10.     int n;
  11.     ll M;
  12.     cin >> n >> M;
  13.     ll P[n];
  14.     for(int i = 0; i < n; i++) {
  15.         cin >> P[i];
  16.     }
  17.     sort(P, P + n);
  18.     ll result = -5e18;
  19.     do{
  20.         ll ans = 0;
  21.         if(P[0] == 0) {
  22.             for(int i = 0; i < n - 1; i++) {
  23.                 ans += llabs(P[i] - P[i + 1]);
  24.             }
  25.             ans += llabs(P[n - 1] - P[0]);
  26.             if(ans <= M) {
  27.                 result = max(result, ans);
  28.             }
  29.         }
  30.      
  31.     }while(next_permutation(P, P + n));
  32.     cout << result << endl;
  33.     return 0;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement