#P5980. Water Mixing Operations

    ID: 19205 Type: Default 1000ms 256MiB

Water Mixing Operations

Water Mixing Operations

You are given infinitely many cups with unlimited capacity and initially n cups of water. The i-th cup initially contains water of volume (l_i) and temperature (a_i).

You are allowed to perform an unlimited number of operations of the following two types:

  1. Splitting: Choose one cup with volume \(V\) and temperature \(T\). Pour its water into several empty cups such that each cup gets water at temperature \(T\) and the total volume poured equals \(V\). Note that the volumes can be any nonnegative real numbers.
  2. Mixing: Choose two cups, one with volume \(V_a\) and temperature \(T_a\), and another with volume \(V_b\) and temperature \(T_b\). Mix them to obtain a single cup containing water of volume \(V_a+V_b\) and temperature \(\displaystyle \frac{V_a\cdot T_a+V_b\cdot T_b}{V_a+V_b}\).
Your goal is to perform a sequence of operations such that, at the end, for every \(i\) \( (1\le i\le n)\), the i-th cup contains water of exactly volume \(l_i\) and temperature \(b_i\).

Determine whether it is possible to achieve the target configuration.

Invariants and Key Observations:
  • The total volume of the water remains unchanged.
  • The total heat (i.e. \(\sum_i l_i \times a_i\)) is invariant under the operations. Therefore, it is necessary that \(\sum_{i=1}^{n} l_i \times a_i = \sum_{i=1}^{n} l_i \times b_i\).
  • Mixing operations yield a weighted average of temperatures. Hence, if a cup originally contains water (\(l_i>0\)), its target temperature \(b_i\) must lie within the range of the initial temperatures of cups with water, i.e. between \(\min\{a_i : l_i>0\}\) and \(\max\{a_i : l_i>0\}\).

If these conditions are satisfied, one can show that the target configuration is achievable. Otherwise, it is impossible.

inputFormat

Input is given in four lines:

  1. The first line contains an integer \(n\) representing the number of cups.
  2. The second line contains \(n\) space-separated real numbers \(l_1, l_2, \dots, l_n\), representing the volumes of the cups.
  3. The third line contains \(n\) space-separated real numbers \(a_1, a_2, \dots, a_n\), representing the initial temperatures.
  4. The fourth line contains \(n\) space-separated real numbers \(b_1, b_2, \dots, b_n\), representing the target temperatures.

outputFormat

Output a single line containing either "YES" if it is possible to achieve the target configuration, or "NO" if it is impossible.

sample

3
10 20 30
1 2 3
1 2 3
YES