Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- const int N = 305;
- int a[N], dp[N][N * 105], dd[N];
- signed main() {
- freopen("half.inp", "r", stdin );
- freopen("half.out", "w", stdout);
- cin.tie(0)->sync_with_stdio(0);
- int m, n;
- cin >> n >> m;
- n *= m;
- int sum = 0;
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- sum += a[i];
- }
- dp[0][0] = 1;
- for (int i = 1; i <= n; i++)
- for (int j = 0; j <= 30000; j++) {
- if (dp[i - 1][j]) dp[i][j] = 1;
- if (dp[i - 1][j - a[i]]) dp[i][j] = 1;
- }
- int t = sum / 2;
- while (!dp[n][t]) t--;
- for (int i = n, j = t; i >= 1; i--) {
- if (dp[i - 1][j - a[i]]) {
- dd[i] = 1;
- j -= a[i];
- }
- }
- for (int i = 1; i <= n; i++) {
- cout << dd[i] << ' ';
- if (i % m == 0) cout << '\n';
- }
- cout << t << ' ' << sum - t << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement