forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(SPOJ)MPILOT.cpp
71 lines (56 loc) · 1.06 KB
/
(SPOJ)MPILOT.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Author: rishab_1128
#include<bits/stdc++.h>
using namespace std;
int dp[10005][5005];
int func(int n, int*cap, int*asst, int diff)
{
if(dp[n][diff]!=-1)
return dp[n][diff];
if(n==0)
return 0;
if(diff==0)
{
//If diff=0 then this ele. has to become an asst. for the upcoming captains
return dp[n][diff]=asst[0]+func(n-1,cap+1,asst+1,1);
}
if(diff==n)
{
//If diff=n then this ele. has to become a captain. for the extra assistants
return dp[n][diff]=cap[0]+func(n-1,cap+1,asst+1,diff-1);
}
else
{
int op1=asst[0]+func(n-1,cap+1,asst+1,diff+1);
int op2=cap[0]+func(n-1,cap+1,asst+1,diff-1);
return dp[n][diff]=min(op1,op2);
}
}
void solve()
{
int n;
cin>>n;
int*cap=new int[n];
int*asst=new int[n];
for(int i=0; i<n; i++)
{
cin>>cap[i]>>asst[i];
}
memset(dp,-1,sizeof(dp));
int ans = func(n,cap,asst,0);
cout << ans;
}
int main()
{
std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
}