This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sort undefined_function // To use stable_sort instead sort
#define bpc __builtin_popcount
#define ull unsigned long long
#define ld double
#define ll long long
#define mp make_pair
#define F first
#define S second
# pragma GCC optimize("O2")
//pragma GCC optimize("my solution")
using namespace __gnu_pbds;
using namespace std;
typedef tree<long long, null_type, less_equal<long long>,
rb_tree_tag, tree_order_statistics_node_update> Tree;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll INF = 9223372036854775807LL;
const ll inf = 2147483647;
const ll MOD = 1e9 + 7; //[998244353, 1e9 + 7, 1e9 + 13]
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a * b / gcd(a, b);}
ll ceil(ll a, ll b) {return (a + b - 1) / b;}
vector<ll> v;
vector<vector<ll>> memo;
ll dp(int i, int k) {
if (i == v.size() - 1)
return 1;
if (memo[i][k] != -1)
return memo[i][k];
ll ans = INF;
if (k > 1)
ans = min(ans, dp(i + 1, k - 1) + 1);
ans = min(ans, dp(i + 1, k) + v[i + 1] - v[i]);
return memo[i][k] = ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
v.resize(n + 1);
memo.resize(n + 1, vector<ll> (k + 1, -1));
for (int i = 1; i <= n; i ++)
cin >> v[i];
cout << dp(1, k) << "\n";
return 0;
}
Compilation message (stderr)
stove.cpp: In function 'long long int dp(int, int)':
stove.cpp:35:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | if (i == v.size() - 1)
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |