-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbalanceddiet.java
More file actions
53 lines (38 loc) · 1.05 KB
/
balanceddiet.java
File metadata and controls
53 lines (38 loc) · 1.05 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
51
52
53
import java.util.*;
public class balanceddiet {
static int[][] K;
public static int knap(int[] nums , int n , int W) {
for (int i = 0; i < K.length; i++)
for (int j = 0; j < K[0].length; j++)
{
if (i == 0 || j == 0)
K[i][j] = 0;
else if (nums[i - 1] > j)
K[i][j] = K[i - 1][j];
else
K[i][j] = Math.max(K[i - 1][j] , nums[i - 1] + K[i - 1][j - nums[i - 1]]);
}
return K[n][W];
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true)
{
int n = scan.nextInt();
if (n == 0)
break;
int[] cals = new int[n];
int sum = 0;
for (int i = 0; i < cals.length; i++)
{
cals[i] = scan.nextInt();
sum += cals[i];
}
K = new int[n + 1][sum / 2 + 1];
int small = knap(cals , n , sum / 2);
int big = sum - small;
System.out.println(big + " " + small);
}
scan.close();
}
}