This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
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 - lazy);
a.pop_back();
a.push_back(x + lazy);
a.push_back(x + lazy);
}
else
{
if (x >= b[0] + lazy)
{
sol += x - (b[0] + lazy);
a.push_back(b[0] + lazy + lazy);
reverse(b.begin(), b.end());
b.pop_back();
b.push_back(x - lazy);
b.push_back(x - lazy);
}
else
{
a.push_back(x + lazy);
b.push_back(x - lazy);
}
}
}
cout << sol << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |