Created
November 11, 2018 16:36
-
-
Save cbdavide/11dc7cc1a1f07aedf7a3f88125faed59 to your computer and use it in GitHub Desktop.
UVa 562 Dividing Coins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
#define F first | |
#define S second | |
#define PB push_back | |
#define endl '\n' | |
typedef long long ll; | |
typedef vector<ll> vll; | |
typedef pair<int, int> ii; | |
typedef vector<ii> vii; | |
typedef vector<int> vi; | |
const int INF = ~(1<<31); | |
int n, total; | |
int dp[107][50005], A[107]; | |
int f(int i, int sum) { | |
if(i == n) return abs(sum - (total - sum)); | |
if(~dp[i][sum]) return dp[i][sum]; | |
int &val = dp[i][sum]; | |
val = min( f( i + 1, sum ), f( i + 1, sum + A[i] ) ); | |
return val; | |
} | |
int main() { | |
ios_base::sync_with_stdio(false); | |
cin.tie(NULL); | |
int t; | |
cin >> t; | |
while(t--) { | |
memset(dp, -1, sizeof dp); | |
cin >> n; | |
for(int i=0; i<n; i++) cin >> A[i]; | |
total = accumulate(A, A + n, 0); | |
cout << f(0, 0) << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment