Advertisement
Guest User

Locked in the Past

a guest
Nov 15th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long llint;
  6. typedef pair <int, int> pi;
  7.  
  8. const int MAXN = 1005;
  9. const llint INF = 1000000000000000000LL;
  10.  
  11. llint n, k, x, y, sol = INF;
  12. llint a[MAXN], val[MAXN];
  13.  
  14. int main () {
  15.     ios_base::sync_with_stdio(false);
  16.     cin.tie(0);
  17.     cin >> n >> k;
  18.     k++;
  19.     for (int i = 1; i <= n; i++) {
  20.         cin >> a[i];
  21.         val[i] = (a[i] - a[i - 1] + k) % k;
  22.         x += val[i];
  23.     }
  24.     sort(val + 1, val + 1 + n);
  25.     for (int i = n; i >= 1; i--) {
  26.         x -= val[i];
  27.         y += k - val[i];
  28.         sol = min(sol, max(x, y));
  29.     }
  30.     cout << sol;
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement