Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 6001;
- typedef pair<int, pair<int, int>> super;
- int n, k, d;
- vector <int> a;
- struct node {
- node() : dp(INT_MAX), pre(-1) {}
- int dp;
- int pre;
- };
- vector <vector<node>> DP;
- void dijkstra() {
- priority_queue < super, vector<super>, greater<super> > que;
- for (int i = 1; i <= n; ++i) {
- DP[1][i].dp = a[i];
- DP[1][i].pre = -1;
- if (1 < k) {
- que.push({DP[1][i].dp,{1,i}});
- }
- }
- while (!que.empty()) {
- pair<int, pair<int, int>> top = que.top();
- que.pop();
- int index = top.second.second;
- int cnt = k - top.second.first - 1;
- int CNT = top.second.first;
- if (DP[CNT][index].dp != top.first) continue;
- int lyl = n - cnt;
- for (int i = index + 1; i <= lyl; ++i) {
- int cost = DP[CNT][index].dp + (i - index - 1)*d + a[i];
- if (DP[CNT + 1][i].dp > cost) {
- DP[CNT + 1][i].dp = cost;
- DP[CNT + 1][i].pre = index;
- if (CNT + 1 < k) {
- que.push({cost,{CNT + 1, i}});
- }
- }
- }
- }
- }
- signed main() {
- scanf("%d %d", &n, &k);
- n += k;
- a.resize(n + 1);
- DP.resize(n + 1);
- for (int i = 1; i <= n; ++i) {
- DP[i].resize(n + 1);
- }
- for (int i = 1; i <= n; ++i) {
- scanf("%d", &a[i]);
- }
- scanf("%d", &d);
- dijkstra();
- int MIN = INT_MAX;
- int q;
- for (int i = k; i <= n; ++i) {
- if (DP[k][i].dp < MIN) {
- MIN = DP[k][i].dp;
- q = i;
- }
- }
- cout << MIN << endl;
- set <int> ans;
- while (k) {
- ans.insert(q);
- q = DP[k][q].pre;
- k--;
- }
- for (auto i : ans) {
- printf("%d ", i);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement