-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11570.cpp
More file actions
28 lines (28 loc) · 723 Bytes
/
11570.cpp
File metadata and controls
28 lines (28 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <bits/stdc++.h>
using namespace std;
int n;
int arr[2001];
int memo[2001][2001]; // memo[x][y]는 마지막 음이 각각x,y일때 최솟값
int f(int x, int y) {
int now = max(x, y) + 1;
if(now == n + 1) return 0;
int &ret = memo[x][y];
if(ret != -1) return ret;
if(x == 0) ret = f(now, y);
else {
ret = abs(arr[x] - arr[now]) + f(now, y);
}
if(y == 0) ret = min(ret, f(x, now));
else ret = min(ret, abs(arr[y] - arr[now]) + f(x, now));
return ret;
}
int main() {
cin >> n;
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) {
memo[i][j] = -1;
}
}
for(int i = 1; i <= n; i++) cin >> arr[i];
cout << f(0, 0);
}