#include <bits/stdc++.h>
using namespace std;
const int N = 510, M = 200010, INF = 1e9, offset = 100005;
int cash[N];
int dp[N][M];
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> cash[i];
sum += cash[i];
}
for (int i = 0; i <= n; i++)
{
for (int j = 0; j < M; j++)
{
dp[i][j] = INF;
}
}
sort(cash + 1, cash + n + 1, greater<int>());
dp[0][offset] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < M; j++)
{
if (dp[i - 1][j] != INF && j + cash[i] < M)
{
dp[i][j + cash[i]] = cash[i] + dp[i - 1][j];
}
if (dp[i - 1][j] != INF && j - cash[i] >= 0)
{
dp[i][j - cash[i]] = cash[i] + dp[i - 1][j];
}
}
}
int mx = 0;
for (int i = 1; i <= n; i++)
{
if (dp[i][offset] != INF)
mx = max(mx, dp[i][offset]);
}
cout << sum - (mx >> 1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |