#include <stdio.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double
const ll maxn = 5e3 + 1, maxm = 1e6 + 1;
const ll mod = 1e9 + 7, inf = 1e9, block = 550, hb = 31, base = 1000050017, biginf = 5e18;
const ld eps = 1e-9;
int n;
int a[maxn];
ll dp[maxn][maxn];;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i){
cin >> a[i];
}
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j){
dp[i][j] = inf * inf;
}
}
dp[2][n - 1] = min(a[2], a[n - 1]);
for (int i = n - 1; i > 1; --i){
for (int j = 1; j + i - 1 <= n; ++j){
int l = j, r = j + i - 1;
if (a[l + 1] <= a[r]){
dp[l + 1][r] = min(dp[l + 1][r], dp[l][r] + abs(a[l + 1] - a[l]));
}
if (a[r - 1] <= a[l]){
dp[l][r - 1] = min(dp[l][r - 1], dp[l][r] + abs(a[r - 1] - a[r]));
}
if (a[l] == a[r] && l + 1 <= r - 1){
int x = min(a[l + 1], a[r - 1]);
dp[l + 1][r - 1] = min(dp[l + 1][r - 1], dp[l][r] + abs(a[l] - x));
}
}
}
ll ans = inf * inf;
for (int i = 1; i <= n; ++i){
ans = min(ans, dp[i][i]);
}
if (ans == inf * inf){
cout << "NO\n";
exit(0);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
2132 KB |
Output isn't correct |
3 |
Incorrect |
9 ms |
12116 KB |
Output isn't correct |
4 |
Incorrect |
77 ms |
82840 KB |
Output isn't correct |
5 |
Incorrect |
221 ms |
196064 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
596 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
3156 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
2260 KB |
Output isn't correct |
3 |
Incorrect |
9 ms |
12164 KB |
Output isn't correct |
4 |
Incorrect |
96 ms |
82836 KB |
Output isn't correct |
5 |
Incorrect |
96 ms |
82832 KB |
Output isn't correct |
6 |
Incorrect |
170 ms |
141644 KB |
Output isn't correct |
7 |
Incorrect |
170 ms |
141604 KB |
Output isn't correct |
8 |
Incorrect |
227 ms |
195980 KB |
Output isn't correct |
9 |
Incorrect |
223 ms |
196128 KB |
Output isn't correct |
10 |
Incorrect |
281 ms |
195952 KB |
Output isn't correct |