Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int N = 2004; //taken according to constraints
- int main()
- {
- int n; cin >> n;
- int a[n];
- for(int i = 0; i < n; i++)
- cin >> a[i];
- int dp[N][N]; memset(dp, 0, sizeof(dp));
- dp[0][0] = 1; //base case, no considerations, no elements in bags
- for(int i = 0; i < n; i++)
- {
- for(int bag1 = N - 1; bag1 >= 0; bag1--)
- {
- for(int bag2 = N - 1; bag2 >= 0; bag2--)
- {
- //put rod in bag1
- if(bag1 + a[i] < N)
- dp[bag1 + a[i]][bag2] |= dp[bag1][bag2];
- //put rod in bag2
- if(bag2 + a[i] < N)
- dp[bag1][bag2 + a[i]] |= dp[bag1][bag2];
- }
- }
- }
- int ans = 0;
- for(int i = 0; i < N; i++)
- if(dp[i][i])
- ans = max(ans, i);
- cout << ans;
- return 0;
- }
Add Comment
Please, Sign In to add comment