406084: GYM102263 F Musical Chairs

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

Description

F. Musical Chairstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Essa is playing Musical Chairs with his friends, Musical chairs, is a game of elimination involving players, chairs, and music, with one fewer chair than players.

When the song ends whichever player fails to sit on a chair is eliminated, with a chair then being removed and the process repeated until only one player remains.

As we know Essa gets really competitive and will be willing to do anything to win, so he memorized each song's length in the playlist and the order they will be played. He also knows the order of the chairs that will be eliminated at the beginning of each turn, now at each beginning of a turn, Essa decides whether the group moves in clockwise or counterclockwise.

Each player changes their position after each second. The chairs will be used to form a circle, so if Essa chooses to move clockwise, each $$$i^{\text{th}}$$$ player will move to $$$i+1$$$ position and the player at the last position will move to the first position. If Essa chooses to move counterclockwise, each $$$i^{\text{th}}$$$ player will move to $$$i-1$$$ position, and the player at the first position will move to the last position. Given Essa's initial position, the length of each song and the order the chairs will be eliminated. Is there a way Essa can win?

Note that each time a chair is removed, the circle gets smaller.

Input

The first line will contain 2 integers $$$n$$$ number of players ($$$2\le n \le 1000$$$) and $$$p$$$ Essa's initial position ($$$1\le p \le n$$$).

The second line will contain $$$n-1$$$ integers, the length of the $$$i^{\text{th}}$$$ song in seconds ($$$1\le ai^{\text{th}} \le 10^9$$$).

The third line will contain $$$n-1$$$ distinct integers $$$j^{\text{th}}$$$ the order that the chairs will be eliminated ($$$1\le aj^{\text{th}} \le n$$$).

Output

print "Yes" without quotations if there's a way Essa can win, otherwise print "No" without quotations.

ExampleInput
5 3
4 4 4 4
4 3 2 1
Output
Yes

Source/Category

加入题单

上一题 下一题 算法标签: