Official

G - Not Too Many Balls Editorial by en_translator


This problem can be boiled down to a maximum flow problem. Specifically, the answer to this problem equals the maximum \(s\) - \(t\) flow on the graph with the following \((N+M+2)\) vertices:

  • vertices \(x_1, x_2, \ldots, x_N\) corresponding to the colors of the balls;
  • vertices \(y_1, y_2, \ldots, y_M\) corresponding to the boxes;
  • source \(s\) and sink \(t\),

and the following edges:

  • type A: edge from \(s\) to \(x_i\) with capacity \(A_i\), for each \(i = 1, 2, \ldots, N\);
  • type B: edge from \(y_j\) to \(t\) with capacity \(B_j\), for each \(j = 1, 2, \ldots, M\);
  • type C: edge from \(x_i\) to \(y_j\) with capacity \(i \times j\), for each \(i = 1, 2, \ldots, N\) and \(j = 1, 2, \ldots, M\).

However, this graph is so large that it is difficult to directly find the maximum flow with typical algorithms like Dinic’s algorithm. Instead, we try to find the minimum \(s\) - \(t\) cut, exploiting the max-flow min-cut theorem.

Let \(X\coloneqq \lbrace x_1, x_2, \ldots, x_N\rbrace, Y \coloneqq \lbrace y_1, y_2, \ldots, y_M\rbrace\); then choosing an \(s\) - \(t\) cut is equivalent to choosing a vertex set \(P \subseteq X\) of those in \(X\) belonging to the \(s\) side, and a vertex set \(Q \subseteq Y\) of those in \(Y\) belonging to the \(s\) side. The capacity of the cut when \(P\) and \(Q\) are chosen is the sum of the contribution of type-A edges \(\sum_{x_i \in X\setminus P} A_i\), that of type-B edges \(\sum_{x_i \in P} \sum_{y_j \in Y\setminus Q} ij\), and that of type-C edges \(\sum_{y_j \in Q} B_j\); so the capacity of the minimum cut is

\[\min_{P \subseteq X} \min_{Q \subseteq Y} \left( \sum_{x_i \in X\setminus P} A_i + \sum_{x_i \in P} i \sum_{y_j \in Y\setminus Q} j + \sum_{y_j \in Q} B_j \right) . \]

Here, considering branches by the value \(k \coloneqq \sum_{x_i \in P}i\), which spans from \(0\) through \(L \coloneqq N(N+1)/2\), we can rewrite the expression above as

\[ \begin{aligned} \min_{0 \leq k \leq L}\min_{\substack{P \subseteq X, \\ \sum_{x_i \in P} i = k}} \min_{Q \subseteq Y} \left( \sum_{x_i \in X\setminus P} A_i + k\sum_{y_j \in Y\setminus Q} j + \sum_{y_j \in Q} B_j \right) &= \min_{0 \leq k \leq L}\left( \min_{\substack{P \subseteq X, \\ \sum_{x_i \in P} i = k}} \sum_{x_i \in X\setminus P} A_i + \min_{Q \subseteq Y} \left(\sum_{y_j \in Y\setminus Q} jk + \sum_{y_j \in Q} B_j \right) \right)\\ &= \min_{0 \leq k \leq L}\left( \min_{\substack{P \subseteq X, \\ \sum_{x_i \in P} i = k}} \sum_{x_i \in X\setminus P} A_i + \sum_{y_j \in Y} \min \lbrace jk, B_j \rbrace \right). \end{aligned} \]

One can find \(\displaystyle \min_{\substack{P \subseteq X, \\\sum_{i \in P} i = k}} \sum_{i \in X\setminus P} A_i\) for all \(k = 0, 1, \ldots, L\) in a total of \(O(NL) = O(N^3)\) time with DP (Dynamic Programming).

One can also find \(\sum_{y_j \in Y} \min\lbrace jk, B_j \rbrace\) for all \(k = 0, 1, \ldots, L\) in ascending order of \(k\). Specifically, start with the initial state where \(jk\) side are adopted for all \(y_j \in Y\). While scanning \(k\) in ascending order, switch to the \(B_j\) side once \(y_j \in Y\) satisfies \(jk \gt B_j\). This way, they can be found in a total of \(O(L+M) = O(N^2+M)\) time.

posted:
last update: