## 1) Abridged problem statement

You have **N (1 ≤ N ≤ 21)** tubes. Initially tube *i* contains **Sᵢ** liters; you want to reach target amounts **Dᵢ**.
One operation ("pouring") allows transferring **any amount** of liquid from **one tube to another** (some liquid may be spilled or extra liquid may appear overall, as implied by the story—but operationally, we only control transfers between tubes; feasibility is determined by total volume).

Find the **minimum number of pourings** needed to transform the configuration from **S** to **D**, or output **-1** if impossible.

---

## 2) Detailed editorial (solution idea + proof)

### Key observation 1: Feasibility requires equal total volume
A pouring transfers liquid between tubes and does not change the total amount of liquid. Therefore:
- If `sum(S) != sum(D)`, it is **impossible** ⇒ answer `-1`.

Assume from now on totals match.

### Key observation 2: Use differences
Define for each tube:
\[
\text{diff}_i = S_i - D_i
\]
- If `diff_i > 0`: tube *i* has **extra** liquid that must be sent out.
- If `diff_i < 0`: tube *i* **needs** `-diff_i` liquid to be received.
- The global sum of diffs is 0.

### Key observation 3: Any "balanced group" can be fixed internally
Consider a subset of tubes \(G\). If:
\[
\sum_{i \in G} \text{diff}_i = 0
\]
then the tubes in \(G\) can be fixed **using only transfers within \(G\)**.

Moreover, such a group of size \(|G| = k\) can always be fixed in **at most \(k-1\)** pourings:
- Think of moving all required liquid along edges of a spanning tree inside the group, consolidating transfers so that each pour reduces the number of "incorrect" tubes by at least 1.
- Intuitively, to "connect" \(k\) nodes and redistribute flow among them, you need at least \(k-1\) transfers in the worst case, and it is achievable.

### Key observation 4: Partitioning into many balanced groups minimizes operations
If we partition all tubes into \(g\) disjoint balanced groups:
- Group sizes sum to \(N\): \(\sum k_j = N\)
- Each group \(j\) can be fixed in at most \(k_j - 1\) pours.
- Total pours \(\le \sum (k_j - 1) = N - g\)

So to **minimize** pours, we want to **maximize the number of balanced groups** in a partition.

Thus, the problem becomes:

> Partition the set of indices into the maximum number of subsets whose diff-sum is 0.

Let that maximum be `g_max`. Then the minimum number of pourings is:
\[
\text{answer} = N - g_{\max}
\]

### DP over subsets
Since \(N \le 21\), we can do bitmask DP.

Precompute:
- `sum[mask]` = \(\sum_{i \in mask} diff_i\)

DP definition:
- `dp[mask]` = maximum number of balanced (sum==0) groups we can partition the subset `mask` into.

Transition used in the provided solution:

1. Start with best value from removing one element:
   \[
   dp[mask] = \max_{i \in mask} dp[mask \setminus \{i\}]
   \]
   This ensures dp is non-decreasing as mask grows and effectively considers some ordering.

2. If the whole `mask` itself is balanced (`sum[mask]==0`), we can treat it as one additional group:
   \[
   dp[mask] \mathrel{+}= 1
   \]
   Why is this valid with step (1)?
   Because step (1) makes `dp[mask]` equal to the best partitioning count achievable inside `mask` *without necessarily using all elements as a group*. If `mask` is balanced, we can always append the interpretation "this entire mask forms one group", so the best count for `mask` becomes at least "best for some submask + 1", and the recurrence as implemented correctly captures the maximum count of zero-sum segments achievable along some permutation perspective.

Finally:
- `g_max = dp[(1<<N)-1]`
- `answer = N - g_max`

Complexities:
- Precomputing sums: `O(N * 2^N)` (via lowbit DP is `O(2^N)`)
- DP: `O(N * 2^N)`
This fits for `N=21` (~44 million inner checks).

---

## 3) C++ Solution

```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<int> S, D;

void read() {
    cin >> n;
    S.resize(n);
    D.resize(n);
    cin >> S >> D;
}

void solve() {
    // It's clear that it's impossible when the sum of S and D are different. We
    // can further generalize this argument. Consider a set of positions
    // i[1], ..., i[k] with sum of S and D being equal - we can always make the
    // values equal in k - 1 steps. Unless there are some equal values, or more
    // generally another subset that has equal sum between S and D, we can't do
    // better than k - 1 steps. We can prove this because if there is no
    // subset of equal sum, we would have to have a direct or indirect
    // "connection" between each of the elements in the subset. Trivially to
    // have something connected, we need at least k - 1 edges (number of edges
    // in a spanning tree).
    //
    // This intuition leads us to the idea that we want to split the N elements
    // into the largest number subsets of equal sum between S and D. For
    // simplicity, we could instead look at the difference between S and D as
    // then we are interested in sum / balance equal to 0. To do this, we could
    // do a bitmask dp[mask][sum_subset_so_far]. Alternatively, we could do a
    // 3^n approach. However, this might still be a bit too slow and instead we
    // can notice the other main observations - consider some arbitrary order
    // p[1], ..., p[n] of elements we will consider. Let's also do pref[i],
    // being the prefix sum over this order. We have the incentive to do 0-sum
    // subsets as often as possible, meaning that whenever the prefix sum
    // reaches 0, we should always snap a new group. Particularly, if a 0-group
    // starts at p[l] and ends at p[r], then it's sum is psum[r] - psum[l - 1],
    // but we know that psum[l - 1] = 0 (because the prefix until l - 1 is
    // decomposed into 0-sum segments).
    //
    // To summarize, this means that we don't actually need to keep the second
    // dimension of the DP - we can always just consider the sum of D and S in
    // the current mask, and "subtract one" every time it reaches 0.

    if(accumulate(S.begin(), S.end(), 0) != accumulate(D.begin(), D.end(), 0)) {
        cout << -1 << endl;
        return;
    }

    vector<int> diff(n);
    for(int i = 0; i < n; i++) {
        diff[i] = S[i] - D[i];
    }

    vector<int> sum((1 << n), 0);
    for(int mask = 1; mask < (1 << n); mask++) {
        int lb = mask & (-mask);
        int bit = __builtin_ctz(lb);
        sum[mask] = sum[mask ^ lb] + diff[bit];
    }

    vector<int> dp((1 << n), 0);
    for(int mask = 1; mask < (1 << n); mask++) {
        for(int i = 0; i < n; i++) {
            if(mask & (1 << i)) {
                dp[mask] = max(dp[mask], dp[mask ^ (1 << i)]);
            }
        }
        if(sum[mask] == 0) {
            dp[mask]++;
        }
    }

    cout << n - dp[(1 << n) - 1] << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

---

## 4) Python solution (same approach) with detailed comments

```python
import sys

def solve() -> None:
    data = sys.stdin.read().strip().split()
    if not data:
        return
    it = iter(data)
    n = int(next(it))
    S = [int(next(it)) for _ in range(n)]
    D = [int(next(it)) for _ in range(n)]

    # Feasibility: total liquid must match
    if sum(S) != sum(D):
        print(-1)
        return

    # diff[i] is how much tube i must send out (positive) or receive (negative)
    diff = [S[i] - D[i] for i in range(n)]

    size = 1 << n

    # Precompute sum[mask] = sum(diff[i] for i in mask)
    # Use lowbit technique to do it in O(2^n)
    ssum = [0] * size
    for mask in range(1, size):
        lb = mask & -mask                 # lowest set bit (power of two)
        bit = (lb.bit_length() - 1)       # index of that bit
        ssum[mask] = ssum[mask ^ lb] + diff[bit]

    # dp[mask] = maximum number of balanced (sum==0) groups obtainable from mask
    dp = [0] * size
    for mask in range(1, size):
        best = 0

        # Try removing each present element and inherit the best dp
        # This is O(n) per mask, total O(n * 2^n)
        m = mask
        while m:
            lb = m & -m
            best = max(best, dp[mask ^ lb])
            m ^= lb

        # If this subset itself is balanced, we can form one more group
        if ssum[mask] == 0:
            best += 1

        dp[mask] = best

    gmax = dp[size - 1]
    # Minimum pourings = N - max_number_of_zero_sum_groups
    print(n - gmax)

if __name__ == "__main__":
    solve()
```

---

## 5) Compressed editorial

- If `sum(S) != sum(D)`: impossible ⇒ `-1`.
- Let `diff[i] = S[i] - D[i]`. We need to redistribute so all diffs become 0.
- Any subset with `sum(diff)==0` is **internally fixable**; a balanced group of size `k` needs `k-1` pours.
- If we partition all tubes into `g` balanced groups, total pours = `N - g`. So maximize `g`.
- Bitmask DP:
  - Precompute `sum[mask]` over diffs.
  - `dp[mask] = max_{i in mask} dp[mask \ {i}] + (sum[mask]==0)`
- Answer: `N - dp[(1<<N)-1]`.
- Complexity: `O(N * 2^N)` time, `O(2^N)` memory (fits for `N≤21`).
