102554: [AtCoder]ABC255 E - Lucky Numbers

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

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

Input

题意翻译

给定长度为 $ n - 1 $ 的序列 $ S $,存在序列 $ a_n $ 满足 $ a_i + a_{i + 1} = S_i $,给定 $ m $ 个幸运数字 $ x_1, x_2, \cdots, x_m $。你需要确定一个合法序列 $ a $ 使其中有最多的数字为幸运数字,求最大值。

Output

得分: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 \leq i \leq N$且$A_i \in \lbrace X_1, X_2, \ldots, X_M \rbrace$的整数$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

9 2
2 3 3 4 -4 -7 -4 -1
-1 5

样例输出1

4

一个好序列$A = (3, -1, 4, -1, 5, -9, 2, -6, 5)$包含四个幸运数字:$A_2, A_4, A_5, A_9$,这是最大的可能数量。


样例输入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

加入题单

算法标签: