Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef pair<int,int> ii;
- #define OO INT_MAX
- #define S second
- #define F first
- const int N = 1e2 + 2e1;
- int n ,Sum ,A[N];
- ii Dp[N][N * 2];
- ii Calc(int i ,int sumA){
- int sumB = Sum - sumA;
- if( i == n + 1 ){
- if( abs(sumA - sumB ) < abs(Dp[n][sumA].F - Dp[n][sumA].S) ){
- Dp[n][sumA].F = sumA;
- Dp[n][sumA].S = sumB;
- }
- return Dp[n][sumA];
- }
- ii &Re = Dp[i][sumA];
- if( Re.S != OO ) return Re;
- ii c1 = Calc(i+1 ,sumA);
- ii c2 = Calc(i+1 , sumA + A[i]);
- if( abs(c1.F - c1.S) < abs(c2.F - c2.S) )
- Re = c1;
- else Re = c2;
- return Re;
- }
- int main() {
- cin >> n;
- for(int i=1 ; i <= n ; i++)
- cin >> A[i] ,Sum += A[i];
- // memset Dp (OO ,0)
- for(int i = 0 ; i < N ; i++){
- for(int j = 0 ; j < N * 2 ; j ++){
- Dp[i][j].F = 0;
- Dp[i][j].S = OO;
- }
- }
- ii Ans = Calc( 1 , 0 );
- cout << Ans.F << ' ' << Ans.S << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement