Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "DOMINOS"
- #include <iostream>
- #include <cstdio>
- #include <cassert>
- using namespace std;
- using ll = long long;
- using ld = long double;
- constexpr int N = 1e3 + 5;
- constexpr int Inf = 1e9 + 7;
- int n, a[N], b[N], sum(0);
- int dp[N][N * 6];
- void Read()
- {
- cin >> n;
- for (int i = 1; i <= n; ++i)
- cin >> a[i], sum += a[i];
- for (int i = 1; i <= n; ++i)
- cin >> b[i], sum += b[i];
- }
- void Solve()
- {
- fill_n(&dp[0][0], N * N * 6, Inf);
- dp[0][0] = 0;
- for (int i = 1; i <= n; ++i)
- for (int j = 0; j <= 6 * (i - 1); ++j)
- if (dp[i - 1][j] < Inf)
- {
- dp[i][j + a[i]] = min(dp[i][j + a[i]], dp[i - 1][j]);
- dp[i][j + b[i]] = min(dp[i][j + b[i]], dp[i - 1][j] + 1);
- }
- int ans(0), res(0);
- for (int i = sum / 2; ~i; --i)
- if (dp[n][i] < Inf)
- {
- ans = i;
- break;
- }
- for (int i = (sum + 1) / 2; i <= sum; ++i)
- if (dp[n][i] < Inf)
- {
- res = i;
- break;
- }
- assert(ans + res == sum);
- cout << abs(ans - res) << " " << min(dp[n][ans], dp[n][res]);
- }
- int32_t main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement