Benchmark AI / Public workspace

Humanity's Last Code Exam / 2016_A / Balanced Diet

Problem

Answer published by the source. Consult the official source to check your work against its answer.

platform

atcoder

question content

## Problem Statement Every day, Danny buys one sweet from the candy store and eats it. The store has m m types of sweets, numbered from 1 to m m . Danny knows that a balanced diet is important and is applying this concept to his sweet purchasing. To each sweet type i i , he has assigned a target fraction, which is a real number fi f_i (0 < fi f_i ≤ 1). He wants the fraction of sweets of type i i among all sweets he has eaten to be roughly equal to fi f_i . To be more precise, let si s_i denote the number of sweets of type i i that Danny has eaten, and let n=i=1msi n = \sum_{i=1}^{m} s_i . We say the set of sweets is balanced if for every i i we have: nfi1<si<nfi+1. n f_i - 1 < s_i < n f_i + 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 f_i 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 m (1 ≤ m m ≤ 10^5), which is the number of types of sweets, and k k (0 ≤ k k ≤ 10^5), which is the number of sweets Danny has already eaten. - The second line contains m m positive integers a1,a2,,am a_1, a_2, \ldots, a_m . These numbers are proportional to f1,f2,,fm f_1, f_2, \ldots, f_m , that is, fi=aij=1maj f_i = \frac{a_i}{\sum_{j=1}^{m} a_j} . It is guaranteed that the sum of all aj a_j is no larger than 10^5. - The third line contains k k integers b1,b2,,bk b_1, b_2, \ldots, b_k (1 ≤ bi b_i m m ), where each bi b_i denotes the type of sweet Danny bought and ate on the i i -th 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 Input 1
Plain-text mathematical notation (without MathML)
## Problem Statement

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 f_(i) (0 < f_(i) ≤ 1). He wants the fraction of sweets of type i among all sweets he has eaten to be roughly equal to f_(i).

To be more precise, let s_(i) denote the number of sweets of type i that Danny has eaten, and let n=∑_(i=1)^(m)s_(i). We say the set of sweets is balanced if for every i we have:

nf_(i)−1<s_(i)<nf_(i)+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 f_(i) 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 ≤ 10^5), which is the number of types of sweets, and k (0 ≤ k ≤ 10^5), which is the number of sweets Danny has already eaten.
- The second line contains m positive integers a₁,a₂,…,a_(m). These numbers are proportional to f₁,f₂,…,f_(m), that is, f_(i)=(a_(i))/(∑_(j=1)^(m)a_(j)). It is guaranteed that the sum of all a_(j) is no larger than 10^5.
- The third line contains k integers b₁,b₂,…,b_(k) (1 ≤ b_(i) ≤ m), where each b_(i) denotes the type of sweet Danny bought and ate on the i-th 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 Input 1

Original LaTeX notation
## Problem Statement

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 \( f_i \) (0 < \( f_i \) ≤ 1). He wants the fraction of sweets of type \( i \) among all sweets he has eaten to be roughly equal to \( f_i \).

To be more precise, let \( s_i \) denote the number of sweets of type \( i \) that Danny has eaten, and let \( n = \sum_{i=1}^{m} s_i \). We say the set of sweets is balanced if for every \( i \) we have:

\[ n f_i - 1 < s_i < n f_i + 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 \( f_i \) 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 \) ≤ 10^5), which is the number of types of sweets, and \( k \) (0 ≤ \( k \) ≤ 10^5), which is the number of sweets Danny has already eaten.
- The second line contains \( m \) positive integers \( a_1, a_2, \ldots, a_m \). These numbers are proportional to \( f_1, f_2, \ldots, f_m \), that is, \( f_i = \frac{a_i}{\sum_{j=1}^{m} a_j} \). It is guaranteed that the sum of all \( a_j \) is no larger than 10^5.
- The third line contains \( k \) integers \( b_1, b_2, \ldots, b_k \) (1 ≤ \( b_i \) ≤ \( m \)), where each \( b_i \) denotes the type of sweet Danny bought and ate on the \( i \)-th 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 Input 1

Code

6 5
2 1 6 3 5 3
1 2 5 3 5

## Sample Output 1

Code

1

## Sample Input 2

Code

6 4
2 1 6 3 5 3
1 2 5 3

## Sample Output 2

Code

forever

question title

Balanced Diet

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import