実行時間制限: 4 sec / メモリ制限: 1024 MB
配点 : 500 点
問題文
長さ N-1 の整数列 S = (S_1, S_2, \ldots, S_{N-1}) および、「ラッキーナンバー」として M 個の相異なる整数 X_1, X_2, \ldots, X_M が与えられます。
長さ N の整数列 A = (A_1, A_2, \ldots, A_N) であって、次の条件を満たすものを「良い数列」と呼びます。
すべての i = 1, 2, \ldots, N-1 について、A_i + A_{i+1} = S_i が成り立つ。
良い数列 A を 1 つ選ぶときの、A の要素のうちラッキーナンバーであるものの個数(すなわち、A_i \in \lbrace X_1, X_2, \ldots, X_M \rbrace となる 1 以上 N 以下の整数 i の個数)としてあり得る最大値を求めてください。
制約
- 2 \leq N \leq 10^5
- 1 \leq M \leq 10
- -10^9 \leq S_i \leq 10^9
- -10^9 \leq X_i \leq 10^9
- X_1 \lt X_2 \lt \cdots \lt X_M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \ldots S_{N-1} X_1 X_2 \ldots X_M
出力
良い数列 A を 1 つ選ぶときの、A の要素のうちラッキーナンバーであるものの個数としてありうる最大値を出力せよ。
入力例 1
9 2 2 3 3 4 -4 -7 -4 -1 -1 5
出力例 1
4
良い数列 A として A = (3, -1, 4, -1, 5, -9, 2, -6, 5) を選ぶと、A の要素のうちラッキーナンバーであるものは A_2, A_4, A_5, A_9 の 4 個となり、これが考えられる中で最大です。
入力例 2
20 10 -183260318 206417795 409343217 238245886 138964265 -415224774 -499400499 -313180261 283784093 498751662 668946791 965735441 382033304 177367159 31017484 27914238 757966050 878978971 73210901 -470019195 -379631053 -287722161 -231146414 -84796739 328710269 355719851 416979387 431167199 498905398
出力例 2
8
Score : 500 points
Problem Statement
You are given a sequence of N-1 integers S = (S_1, S_2, \ldots, S_{N-1}), and M distinct integers X_1, X_2, \ldots, X_M, which are called lucky numbers.
A sequence of N integers A = (A_1, A_2, \ldots, A_N) satisfying the following condition is called a good sequence.
A_i + A_{i+1} = S_i holds for every i = 1, 2, \ldots, N-1.
Find the maximum possible number of terms that are lucky numbers in a good sequence A, that is, the maximum possible number of integers i between 1 and N such that A_i \in \lbrace X_1, X_2, \ldots, X_M \rbrace.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq M \leq 10
- -10^9 \leq S_i \leq 10^9
- -10^9 \leq X_i \leq 10^9
- X_1 \lt X_2 \lt \cdots \lt X_M
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M S_1 S_2 \ldots S_{N-1} X_1 X_2 \ldots X_M
Output
Print the maximum possible number of terms that are lucky numbers in a good sequence A.
Sample Input 1
9 2 2 3 3 4 -4 -7 -4 -1 -1 5
Sample Output 1
4
A good sequence A = (3, -1, 4, -1, 5, -9, 2, -6, 5) contains four terms that are lucky numbers: A_2, A_4, A_5, A_9, which is the maximum possible count.
Sample Input 2
20 10 -183260318 206417795 409343217 238245886 138964265 -415224774 -499400499 -313180261 283784093 498751662 668946791 965735441 382033304 177367159 31017484 27914238 757966050 878978971 73210901 -470019195 -379631053 -287722161 -231146414 -84796739 328710269 355719851 416979387 431167199 498905398
Sample Output 2
8