-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa10690_Expression_Again.cpp
74 lines (70 loc) · 1.83 KB
/
UVa10690_Expression_Again.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
72
73
74
#include <iostream>
using namespace std;
void findMaxMin(int[], int, int, int);
int main()
{
int n, m;
while (cin >> n >> m)
{
int numbers[n + m];
int sum = 0;
for (int i = 0; i < n + m; i++)
{
cin >> numbers[i];
sum += numbers[i];
}
findMaxMin(numbers, n, m, sum);
}
}
void findMaxMin(int numbers[], int n, int m, int sum)
{
bool avaliable[n + 1][5001] = {};
avaliable[0][2500] = true;
// find all the possible sum in the first parenthesis
for (int t = 0; t < n + m; t++)
{
// if t + 1 < n, avaliable[n][?] can't be ture
for (int i = (t + 1 >= n) ? n - 1 : t; i >= 0; i--)
{
if (numbers[t] > 0)
{
int max = 5000 - numbers[t];
for (int j = 0; j <= max; j++)
{
if (avaliable[i][j])
{
avaliable[i + 1][j + numbers[t]] = true;
}
}
}
else
{
for (int j = -numbers[t]; j <= 5000; j++)
{
if (avaliable[i][j])
{
avaliable[i + 1][j + numbers[t]] = true;
}
}
}
}
}
// find answer
int max = -2500 * 2500, min = 2500 * 2500;
for (int i = 0; i <= 5000; i++)
{
if (avaliable[n][i])
{
int product = (i - 2500) * (sum - i + 2500);
if (product > max)
{
max = product;
}
if (product < min)
{
min = product;
}
}
}
cout << max << " " << min << endl;
}