201505: [AtCoder]ARC150 F - Constant Sum Subsequence

Memory Limit:1024 MB Time Limit:5 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $1000$ points

Problem Statement

We have a sequence of positive integers of length $N^2$, $A=(A_1,\ A_2,\ \dots,\ A_{N^2})$, and a positive integer $S$. For this sequence of positive integers, $A_i=A_{i+N}$ holds for positive integers $i\ (1\leq i \leq N^2-N)$, and only $A_1,\ A_2,\ \dots,\ A_N$ are given as the input.

Find the minimum integer $L$ such that every sequence $B$ of positive integers totaling $S$ is a (not necessarily contiguous) subsequence of the sequence $(A_1,\ A_2,\ \dots,\ A_L)$ of positive integers.

It can be shown that at least one such $L$ exists under the Constraints of this problem.

Constraints

  • $1 \leq N \leq 1.5 \times 10^6$
  • $1 \leq S \leq \min(N,2 \times 10^5)$
  • $1 \leq A_i \leq S$
  • For every positive integer $x\ (1\leq x \leq S)$, $(A_1,\ A_2,\ \dots,\ A_N)$ contains at least one occurrence of $x$.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

$N$ $S$
$A_1$ $A_2$ $\dots$ $A_N$

Output

Print the answer.


Sample Input 1

6 4
1 1 2 1 4 3

Sample Output 1

9

There are eight sequences $B$ to consider: $B=(1,\ 1,\ 1,\ 1),\ (1,\ 1,\ 2),\ (1,\ 2,\ 1),\ (1,\ 3),\ (2,\ 1,\ 1),\ (2,\ 2),\ (3,\ 1),\ (4)$.

For $L=8$, for instance, $B=(2,\ 2)$ is not a subsequence of $(A_1,A_2,\ \dots,\ A_8)=(1,\ 1,\ 2,\ 1,\ 4,\ 3,\ 1,\ 1)$.

For $L=9$, on the other hand, every $B$ is a subsequence of $(A_1,A_2,\ \dots,\ A_9)=(1,\ 1,\ 2,\ 1,\ 4,\ 3,\ 1,\ 1,\ 2)$.


Sample Input 2

14 5
1 1 1 2 3 1 2 4 5 1 1 2 3 1

Sample Output 2

11

Sample Input 3

19 10
1 6 2 7 4 8 5 9 1 10 4 1 3 1 3 2 2 2 1

Sample Output 3

39

Input

题意翻译

有一个长度为 $n^2$ 的序列 $\{ a_i \}$ 以及一个整数 $sum$。 对于 $\forall i\in[1,n^2-n]$,这个序列满足 $a_i=a_{i+n}$,在本题中只给出 $n$ 个数 $\{ a_1,\dots,a_n\}$。 现在要求你找到一个最小的整数 $p$,使得所有满足 $\sum_{i} b_i=sum$ 的序列 $\{b_i\}$ 是 $\{a_1,\dots,a_p\}$ 的子序列。

加入题单

算法标签: