-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressionAgain_UVA10690OJ.cpp
64 lines (50 loc) · 1.38 KB
/
expressionAgain_UVA10690OJ.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
while (true)
{
int n = 0, m = 0; cin >> n >> m;
if (n + m == 0) break;
if (n > m) swap(n, m);
int s = n + m;
vector<int> a(s);
for (int i = 0; i <= s - 1; ++i)
{
cin >> a[i];
}
sort(a.begin(), a.end());
int total = 0;
vector<vector<bool>> dp(n + 1, vector<bool>(5001, false));
dp[0][2500 + 0] = 1;
for (int i = 1; i <= s; ++i)
{
total += a[i - 1];
for (int j = min(i, n); j >= 1; --j)
{
for (int k = -2500; k <= 2500; ++k)
{
if (dp[j - 1][2500 + k] > 0)
{
dp[j][2500 + k + a[i - 1]] = true;
}
}
}
}
int ansMax = INT_MIN, ansMin = INT_MAX;
for (int k = -2500; k <= 2500; ++k)
{
if (dp[n][k + 2500] == true)
{
int now = k * (total - k);
ansMax = max(ansMax, now);
ansMin = min(ansMin, now);
}
}
cout << ansMax << ' ' << ansMin << '\n';
}
cout.flush();
return 0;
}