//
// main.cpp
// Modified Knapsack
//
// Created by Himanshu on 29/10/18.
// Copyright © 2018 Himanshu. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
int solve(vector<int> a, int K) {
int n = (int)a.size(), totalDistance = 0;
int dp[n+1][K+1], caveTaken[n+1][K+1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= K; j++) {
dp[i][j] = 0;
caveTaken[i][j] = -1;
}
}
for (int i = 0; i < n; i++) {
totalDistance = totalDistance + a[i];
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= K; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else {
dp[i][j] = dp[i-1][j];
// If optimal solution include current cave
if (dp[i-1][j] < (a[i-1] + dp[i-1][j-1])) {
// If current optimal solution
// include previous cave
if (caveTaken[i-1][j-1] != -1) {
if (i > 1) {
dp[i][j] = max(dp[i-1][j], a[i-1] + dp[i-2][j-1]);
if (dp[i][j] != dp[i-1][j]) {
caveTaken[i][j] = i-1;
}
}
} // If current optimal solution doesn't include
// previous cave
else {
dp[i][j] = a[i-1] + dp[i-1][j-1];
caveTaken[i][j] = i-1;
}
}
}
}
}
return (totalDistance - dp[n][K]);
}
int main(int argc, const char * argv[]) {
vector<int> a = {10, 12, 11, 18};
int K = 2;
cout<<"Minimum distance to travel: "<<solve(a, K)<<endl;
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBNb2RpZmllZCBLbmFwc2FjawovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAyOS8xMC8xOC4KLy8gIENvcHlyaWdodCDCqSAyMDE4IEhpbWFuc2h1LiBBbGwgcmlnaHRzIHJlc2VydmVkLgovLwoKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IHNvbHZlKHZlY3RvcjxpbnQ+IGEsIGludCBLKSB7CiAgICBpbnQgbiA9IChpbnQpYS5zaXplKCksIHRvdGFsRGlzdGFuY2UgPSAwOwogICAgaW50IGRwW24rMV1bSysxXSwgY2F2ZVRha2VuW24rMV1bSysxXTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDw9IG47IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDw9IEs7IGorKykgewogICAgICAgICAgICBkcFtpXVtqXSA9IDA7CiAgICAgICAgICAgIGNhdmVUYWtlbltpXVtqXSA9IC0xOwogICAgICAgIH0KICAgIH0KICAgIAogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICB0b3RhbERpc3RhbmNlID0gdG90YWxEaXN0YW5jZSArIGFbaV07CiAgICB9CiAgICAKICAgIGZvciAoaW50IGkgPSAwOyBpIDw9IG47IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDw9IEs7IGorKykgewogICAgICAgICAgICBpZiAoaSA9PSAwIHx8IGogPT0gMCkgewogICAgICAgICAgICAgICAgZHBbaV1bal0gPSAwOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZHBbaV1bal0gPSBkcFtpLTFdW2pdOwogICAgICAgICAgICAgICAgLy8gSWYgb3B0aW1hbCBzb2x1dGlvbiBpbmNsdWRlIGN1cnJlbnQgY2F2ZQogICAgICAgICAgICAgICAgaWYgKGRwW2ktMV1bal0gPCAoYVtpLTFdICsgZHBbaS0xXVtqLTFdKSkgewogICAgICAgICAgICAgICAgICAgIC8vIElmIGN1cnJlbnQgb3B0aW1hbCBzb2x1dGlvbgogICAgICAgICAgICAgICAgICAgIC8vIGluY2x1ZGUgcHJldmlvdXMgY2F2ZQogICAgICAgICAgICAgICAgICAgIGlmIChjYXZlVGFrZW5baS0xXVtqLTFdICE9IC0xKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpID4gMSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgZHBbaV1bal0gPSBtYXgoZHBbaS0xXVtqXSwgYVtpLTFdICsgZHBbaS0yXVtqLTFdKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChkcFtpXVtqXSAhPSBkcFtpLTFdW2pdKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY2F2ZVRha2VuW2ldW2pdID0gaS0xOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfSAvLyBJZiBjdXJyZW50IG9wdGltYWwgc29sdXRpb24gZG9lc24ndCBpbmNsdWRlCiAgICAgICAgICAgICAgICAgICAgICAvLyBwcmV2aW91cyBjYXZlCiAgICAgICAgICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGRwW2ldW2pdID0gYVtpLTFdICsgZHBbaS0xXVtqLTFdOwogICAgICAgICAgICAgICAgICAgICAgICBjYXZlVGFrZW5baV1bal0gPSBpLTE7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXR1cm4gKHRvdGFsRGlzdGFuY2UgLSBkcFtuXVtLXSk7Cn0KCmludCBtYWluKGludCBhcmdjLCBjb25zdCBjaGFyICogYXJndltdKSB7CiAgICB2ZWN0b3I8aW50PiBhID0gezEwLCAxMiwgMTEsIDE4fTsKICAgIGludCBLID0gMjsKICAgIGNvdXQ8PCJNaW5pbXVtIGRpc3RhbmNlIHRvIHRyYXZlbDogIjw8c29sdmUoYSwgSyk8PGVuZGw7CiAgICByZXR1cm4gMDsKfQo=