#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N, H;
cin >> N >> H;
vector<long long> S(N);
for (int i = 0; i < N; ++i) {
cin >> S[i];
}
if (N == 0) {
cout << 0 << endl;
return 0;
}
long long prev = S[0];
long long sum = 0;
for (int i = 1; i < N; ++i) {
long long low = prev - H;
long long high = prev + H;
long long current = S[i];
long long new_val;
if (current < low) {
new_val = low;
} else if (current > high) {
new_val = high;
} else {
new_val = current;
}
sum += abs(new_val - current);
prev = new_val;
}
cout << sum << endl;
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... |