Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- const long long INF = 1e18 + 7;
- const int MAXN = 1e2 + 100;
- const int N = 1e5 + 10;
- int beam, n;
- vector<int> a;
- array<array<int, MAXN>, MAXN> dp;
- int rec(int l, int r) {
- if (dp[l][r] != INF) {
- return dp[l][r];
- }
- int ans = INF;
- for (int m = l + 1; m < r; ++m) {
- ans = min(ans, rec(l, m) + rec(m, r) + a[r] - a[l]);
- }
- return dp[l][r] = ans;
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- cin >> beam >> n;
- a.resize(n + 2);
- a[0] = 0;
- for (int i = 1; i <= n; ++i) {
- cin >> a[i];
- }
- a[n + 1] = beam;
- n += 2;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- dp[i][j] = INF;
- }
- }
- for (int i = 0; i < n - 1; ++i) {
- dp[i][i + 1] = 0;
- }
- cout << rec(0, n - 1) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment