제출 #668421

#제출 시각아이디문제언어결과실행 시간메모리
668421600MihneaSafety (NOI18_safety)C++17
16 / 100
2077 ms756 KiB
#include <bits/stdc++.h>


using namespace std;

#define int long long


signed main()
{
#ifdef ONPC
  freopen ("input.txt", "r", stdin);
#endif // ONPC

#ifndef ONPC
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC

  int n, dmax, sol = 0, lazy = 0;
  cin >> n >> dmax;

  vector<int> a, b;

  for (int i = 0; i < n; i++)
  {
    int x;
    cin >> x;
    if (i == 0)
    {
      a.push_back(x);
      b.push_back(x);
      continue;
    }
    lazy += dmax;
    for (auto &it : b)
    {
      it += dmax;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());

    if (x <= a.back() - lazy)
    {
      sol += a.back() - lazy - x;
      b.push_back(a.back() - lazy);
      a.pop_back();
      a.push_back(x + lazy);
      a.push_back(x);
    }
    else
    {
      if (x >= b[0])
      {
        sol += x - b[0];
        a.push_back(b[0] + lazy);
        reverse(b.begin(), b.end());
        b.pop_back();
        b.push_back(x);
        b.push_back(x);
      }
      else
      {
        a.push_back(x + lazy);
        b.push_back(x);
      }
    }
  }
  cout << sol << "\n";


  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...