-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonkvisit.cpp
More file actions
50 lines (41 loc) · 1.07 KB
/
Copy pathmonkvisit.cpp
File metadata and controls
50 lines (41 loc) · 1.07 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/monk-visits-coderland-4/
#include <bits/stdc++.h>
using namespace std;
struct Calc
{
int N;
vector<int> c;
vector<int> l;
// step i, Petrol with me, Total petrol required
int minc(int i, int P, int T)
{
cout << i << " " << P << " " << T << "\n";
if (i == N) return 0;
int r = max(0, l[i] - P); // min required
int mi(INT_MAX);
for (int b = r; b <= T; ++b)
{
int cost = b*c[i] + minc(i+1, P+b-l[i], T-b);
mi = min(mi, cost);
}
return mi;
}
};
int main(void)
{
int T;
cin >> T;
for (int t = 0; t < T; ++t)
{
Calc c;
cin >> c.N;
c.c.reserve(c.N); c.l.reserve(c.N);
copy_n(istream_iterator<int>(cin), c.N, back_inserter(c.c));
copy_n(istream_iterator<int>(cin), c.N, back_inserter(c.l));
t = 0;
for (int l: c.l)
t += l;
cout << c.minc(0, 0,t) << "\n";
}
return 0;
}