ICPC 2016
ICPC 2016

A. Balanced Diet

Every day, Danny buys one sweet from the candy store and eats it. The store has m types of sweets, numbered from 1 to m. Danny knows that a balanced diet is important and is applying this concept to his sweet purchasing. To each sweet type i, he has assigne...

Updated May 21, 2026
Track ICPC
Year 2016
Statement Text + PDF
TeXC++Statement textStatement PDF

Problem Statement

Formatted from the contest statement text, with sample tests broken out into copyable blocks.

Time limit 2 seconds
Every day, Danny buys one sweet from the candy store and eats it. The store has m types of sweets,
numbered from 1 to m. Danny knows that a balanced diet is important and is applying this concept to
his sweet purchasing. To each sweet type i, he has assigned a target fraction, which is a real number fi
(0 < fi ≤ 1). He wants the fraction of sweets of type i among all sweets he has eaten to be roughly
        fi . To be more precise, let si denote the number of sweets of type i that Danny has eaten, and
equal toP
let n = m  i=1 si . We say the set of sweets is balanced if for every i we have
                                           nfi − 1 < si < nfi + 1.
Danny has been buying and eating sweets for a while and during this entire time the set of sweets
has been balanced. He is now wondering how many more sweets he can buy while still fulfilling this
condition. Given the target fractions fi and the sequence of sweets he has eaten so far, determine how
many more sweets he can buy and eat so that at any time the set of sweets is balanced.

Input

The input consists of three lines. The first line contains two integers m (1 ≤ m ≤ 105 ), which is the
number of types of sweets, and k (0 ≤ k ≤ 105 ), which is the number of sweets Danny has already
eaten.
The second line contains m positive integers a1 , . . . , am . These numbers are proportional to f1 , . . . , fm ,
that is, fi = Pm      . It is guaranteed that the sum of all aj is no larger than 105 .
               j=1 j

The third line contains k integers b1 , . . . , bk (1 ≤ bi ≤ m), where each bi denotes the type of sweet Danny bought and ate on the ith day. It is guaranteed that every prefix of this sequence (including the whole sequence) is balanced.

Output

Display the maximum number of additional sweets that Danny can buy and eat while keeping his diet continuously balanced. If there is no upper limit on the number of sweets, display the word forever.

Sample Tests

Sample 1
Sample Input
 6 5
 2 1 6 3 5 3
 1 2 5 3 5
Sample Output
1
Sample 2
Sample Input
 6 4
 2 1 6 3 5 3
 1 2 5 3
Sample Output
forever

Editorial

The solution write-up is rendered from the LaTeX source, with equations kept live through MathJax.

Key Observations

  • Write the structural observations that make the problem tractable.

  • State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.

  • If the constraints matter, explain exactly which part of the solution they enable.

Algorithm

  1. Describe the data structures and the state maintained by the algorithm.

  2. Explain the processing order and why it is sufficient.

  3. Mention corner cases explicitly if they affect the implementation.

Correctness Proof

We prove that the algorithm returns the correct answer.

Lemma 1.

State the first key claim.

Proof.

Provide a concise proof.

Lemma 2.

State the next claim if needed.

Proof.

Provide a concise proof.

Theorem.

The algorithm outputs the correct answer for every valid input.

Proof.

Combine the lemmas and finish the argument.

Complexity Analysis

State the running time and memory usage in terms of the input size.

Implementation Notes

  • Mention any non-obvious implementation detail that is easy to get wrong.

  • Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.

Code

C++ solution used for this page.

C++

Clean code view with a raw-file link when you want the original source.

Raw file
#include <bits/stdc++.h>
using namespace std;

namespace {

void solve() {
    // Fill in the full solution logic for the problem here.
}

}  // namespace

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

    solve();
    return 0;
}

Source Files and Assets

Raw files are still available here when you want the original TeX, C++, or statement assets.

Show raw files