-
Notifications
You must be signed in to change notification settings - Fork 12
/
CHOCOLA.cpp
42 lines (38 loc) · 836 Bytes
/
CHOCOLA.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
/*
AUTHOR: Akhilesh Anandh
Solution for "Chocolate" (www.spoj.com/problems/CHOCOLA)
Alorithm: DP
*/
#include<cstdio>
#include<algorithm>
using namespace std;
#define min(a,b) (((a)<(b))?(a):(b))
typedef long long LL;
bool comp(LL a, LL b){ return a>b;}
LL x[1001],y[1001],dp[1001][1001];
int main(){
LL i,j,m,n,t;
for(scanf("%lld",&t);t--;){
scanf("%lld %lld",&m,&n);
m--;
n--;
for(i=1;i<=m;i++)
scanf("%lld",x+i);
for(i=1;i<=n;i++)
scanf("%lld",y+i);
sort(x+1,x+1+m,comp);
sort(y+1,y+n+1,comp);
dp[0][0] = 0;
for(i=1;i<=m;i++) dp[i][0] = x[i] + dp[i-1][0];
for(i=1;i<=n;i++) dp[0][i] = y[i] + dp[0][i-1];
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
LL t1 = dp[i-1][j] + x[i]*(j+1);
LL t2 = dp[i][j-1] + y[j]*(i+1);
dp[i][j] = min(t1,t2);
}
}
printf("%lld\n",dp[m][n]);
}
return 0;
}