This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MAX_S = 1e5 + 42;
const int MAX_N = 512;
const int INF = 1e9;
const int mod = 1e9 + 7;
int dp[MAX_N][MAX_S/2];
int a[MAX_N];
int sum;
int n;
int sol( int pos, int diff ){
if( diff > sum / 2 ) return -INF;
if( dp[pos][diff] != -1 ) return dp[pos][diff];
if( pos == n ){
if( diff == 0 ) return 0;
return -INF;
}
int currNas = 0;
currNas = std::max({ sol(pos+1, diff), sol(pos+1, diff + a[pos]) + a[pos], sol( pos+1, abs( diff - a[pos] ) ) + std::max(0, a[pos] - diff) } );
return dp[pos][diff] = currNas;
}
int main () {
std::cin>>n;
for( int i=0 ; i<n ; i++ ){
std::cin>>a[i];
sum += a[i];
}
std::memset( dp, -1, sizeof(dp) );
int nas = sol( 0, 0 );
//std::cerr<<" ! "<<nas<<"\n";
nas += ( sum - nas*2 );
std::cout<<nas<<"\n";
return 0;
}
# | 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... |