Submission #520039

#TimeUsernameProblemLanguageResultExecution timeMemory
520039sumit_kk10Safety (NOI18_safety)C++17
7 / 100
825 ms2088 KiB
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ll long long
#define F first 
#define S second
using namespace std;
const int N = 2e5 + 5, MOD = 1e9 + 7;
int n, h, a[N];

void solve(){
	cin >> n >> h;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	long long res = INT_MAX; 
	for(int str = 0; str <= 5000; ++str){
		long long pre = str, ans = abs(str - a[1]);
		for(int j = 2; j <= n; ++j){
			if(a[j] > pre + h){
				ans += (a[j] - pre - h);
				pre += h;
			}
			else if(a[j] < pre - h){
				ans += (pre - a[j] - h);
				pre -= h;
			}
			else
				pre = a[j];
			pre = max(pre, 0ll);
		}
		if(ans < res){
			res = ans;
			// cout << str << endl;
		}
	}
	cout << res << '\n';
}

int main() {
    fast;
    int t = 1;
    // cin >> t;
    while(t--)
        solve();
    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...