#P9817. Maximum Chaotic Value
Maximum Chaotic Value
Maximum Chaotic Value
We are given two positive integers n and k. A non‐empty sequence p1, p2, …, pm of length m is called chaotic if and only if it satisfies the following conditions:
- The sum of all its elements does not exceed n, i.e. \(\sum_{i=1}^{m}p_i \le n\).
- For every element \(p_i\), either \(p_i=1\) or \(p_i\) is a prime number.
For a chaotic sequence, its chaotic value is defined as the sum of the squares of each element minus \(k\), that is:
\[ \text{Chaotic Value} = \sum_{i=1}^{m}(p_i-k)^2. \]In particular, any sequence that is not chaotic has a chaotic value of 0.
Your task is: given n and k, find the maximum chaotic value among all sequences. Note that you are allowed to choose a non‐chaotic sequence, which yields a value of 0. In other words, the answer is the maximum between 0 and the maximum achievable value over all chaotic sequences.
Input Constraints: The input consists of two positive integers n and k.
inputFormat
The input contains two space-separated positive integers n and k.
outputFormat
Output a single integer, the maximum chaotic value achievable.
sample
3 1
4