Submission #1288638

#TimeUsernameProblemLanguageResultExecution timeMemory
1288638yonatanlSafety (NOI18_safety)C++20
13 / 100
2098 ms36864 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define upmax(a, b) a = max(a, b)
#define upmin(a, b) a = min(a, b)

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

//const ll MAX_H = 5005;

void solve() {
	ll n, H;
	cin >> n >> H;
	vll arr(n);
	rep(i, 0, n) cin >> arr[i];
	ll MAX_H = 2 * (*(max_element(arr.begin(), arr.end())));
	vvll dp(n, vll(MAX_H, INF));
	rep(j, 0, MAX_H) {
		dp[0][j] = abs(arr[0] - j);
	}
	rep(i, 1, n) {
		rep(j, 0, MAX_H) {
			rep(k, max(j - H, (ll)0), min(MAX_H, j + H + 1)) {
				upmin(dp[i][j], dp[i - 1][k] + abs(arr[i] - j));
			}
		}
	}
	ll res = INF;
	rep(j, 0, MAX_H) upmin(res, dp[n - 1][j]);
	cout << res << endl;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	solve();
}
#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...