#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef pair<int, int> ii;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
int dp[2][N];
int a[N], n, sum = 0;
bool lst = 0, now = 1;
void maximize(int &a, const int &b)
{
if(a < b) a = b;
}
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i], sum += a[i];
for(int i = 0; i <= sum; i++)
dp[0][i] = dp[1][i] = -1;
dp[0][0] = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= sum; j++)
dp[now][j] = -1;
for(int j = 0; j <= sum; j++) if(dp[lst][j] != -1)
{
int team1 = dp[lst][j];
int team2 = dp[lst][j] - j;
//team1
int newt1 = team1 + a[i];
if(newt1 > team2)
maximize(dp[now][newt1 - team2], newt1);
else
maximize(dp[now][team2 - newt1], team2);
//team2
int newt2 = team2 + a[i];
if(newt2 > team1)
maximize(dp[now][newt2 - team1], newt2);
else
maximize(dp[now][team1 - newt2], team1);
// team1 nor team2
maximize(dp[now][j], dp[lst][j]);
}
swap(lst, now);
}
cout << dp[lst][0] + (sum - dp[lst][0] * 2);
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
1108 KB |
Output is correct |
2 |
Correct |
24 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
1108 KB |
Output is correct |
2 |
Correct |
50 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
1108 KB |
Output is correct |
2 |
Correct |
132 ms |
1108 KB |
Output is correct |