message stringlengths 2 65.1k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.
R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.
The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.
Input
The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly.
Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.
Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.
Output
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
Examples
Input
3 4 5
1 1 1 1
1 0 1 1
1 1 0 0
1 1
3 1
1 3
2 4
3 2
Output
3 3 1
Input
4 3 4
0 1 1
1 0 1
1 1 1
0 0 0
1 2
2 1
3 1
1 3
Output
0 2 3 0
Submitted Solution:
```
import sys
import math
import collections
import heapq
input=sys.stdin.readline
n,m,k=(int(i) for i in input().split())
l=[]
l2=[0]*m
for i in range(n):
l1=[int(i) for i in input().split()]
l.append(l1)
l3=[0]*n
for i in range(k):
x,y=(int(i) for i in input().split())
l3[x-1]+=1
l2[y-1]+=1
for i in range(n):
s=0
for j in range(m):
if(l[i][j]==1):
s+=l2[j]
l3[i]=s-l3[i]
print(*l3)
``` | instruction | 0 | 39,947 | 14 | 79,894 |
Yes | output | 1 | 39,947 | 14 | 79,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.
R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.
The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.
Input
The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly.
Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.
Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.
Output
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
Examples
Input
3 4 5
1 1 1 1
1 0 1 1
1 1 0 0
1 1
3 1
1 3
2 4
3 2
Output
3 3 1
Input
4 3 4
0 1 1
1 0 1
1 1 1
0 0 0
1 2
2 1
3 1
1 3
Output
0 2 3 0
Submitted Solution:
```
n, m, k = map(int, input().split())
chats_ = []
for i in range(n):
a = list(map(int, input().split()))
chats_.append(a)
sent = [0 for i in range(n)]
chats = [0 for i in range(m)]
for i in range(k):
a, b = map(int, input().split())
sent[a - 1] += 1
chats[b - 1] += 1
for i in range(n):
sum = 0
for j in range(m):
sum += chats_[i][j] * chats[j]
sum -= sent[i]
print(sum, end = ' ')
``` | instruction | 0 | 39,948 | 14 | 79,896 |
Yes | output | 1 | 39,948 | 14 | 79,897 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.
R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.
The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.
Input
The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly.
Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.
Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.
Output
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
Examples
Input
3 4 5
1 1 1 1
1 0 1 1
1 1 0 0
1 1
3 1
1 3
2 4
3 2
Output
3 3 1
Input
4 3 4
0 1 1
1 0 1
1 1 1
0 0 0
1 2
2 1
3 1
1 3
Output
0 2 3 0
Submitted Solution:
```
# link: https://codeforces.com/problemset/problem/413/B
import sys
if __name__ == "__main__":
i = sys.stdin.readline
n,m,k = tuple(map(int, i().split()))
info = [[] for i in range(n)]
for j in range(n):
info[j] = tuple(map(int, i().split()))
employees = [0] * (n)
chats = [0] * (m)
for j in range(k):
ei, ci = map(int, input().split())
employees[ei-1] -= 1
chats[ci-1] += 1
for i in range(n):
for j in range(m):
employees[i] += chats[j] * info[i][j]
print(*employees)
``` | instruction | 0 | 39,949 | 14 | 79,898 |
Yes | output | 1 | 39,949 | 14 | 79,899 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.
R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.
The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.
Input
The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly.
Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.
Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.
Output
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
Examples
Input
3 4 5
1 1 1 1
1 0 1 1
1 1 0 0
1 1
3 1
1 3
2 4
3 2
Output
3 3 1
Input
4 3 4
0 1 1
1 0 1
1 1 1
0 0 0
1 2
2 1
3 1
1 3
Output
0 2 3 0
Submitted Solution:
```
n, m, k = map(int, input().split())
a, nc, mc = [list(map(int, input().split())) for i in range(n)], [0] * n, [0] * m
for i in range(k):
x, y = map(int, input().split())
nc[x - 1] += 1
mc[y - 1] += 1
print(' '.join(map(str, (sum(mc[j] for j in range(m) if a[i][j]) - nc[i] for i in range(n)))))
``` | instruction | 0 | 39,950 | 14 | 79,900 |
Yes | output | 1 | 39,950 | 14 | 79,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.
R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.
The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.
Input
The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly.
Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.
Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.
Output
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
Examples
Input
3 4 5
1 1 1 1
1 0 1 1
1 1 0 0
1 1
3 1
1 3
2 4
3 2
Output
3 3 1
Input
4 3 4
0 1 1
1 0 1
1 1 1
0 0 0
1 2
2 1
3 1
1 3
Output
0 2 3 0
Submitted Solution:
```
n,m,k=input().split()
n=int(n)
m=int(m)
k=int(k)
l=[]
for i in range(n):
l.append(list(map(int,input().split())))
t=[[0]*2]*k
e=[0]*n
c=[0]*m
for i in range(k):
t0,t1=map(int,input().split())
t[0][0]=t0
t[0][1]=t1
e[t0-1]-=1
c[t0-1]+=1
p=""
for i in range(n):
for j in range(m):
if(l[i][j]):
e[i]=e[i]+c[j]
p=p+str(e[i])+" "
print(p)
``` | instruction | 0 | 39,951 | 14 | 79,902 |
No | output | 1 | 39,951 | 14 | 79,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.
R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.
The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.
Input
The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly.
Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.
Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.
Output
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
Examples
Input
3 4 5
1 1 1 1
1 0 1 1
1 1 0 0
1 1
3 1
1 3
2 4
3 2
Output
3 3 1
Input
4 3 4
0 1 1
1 0 1
1 1 1
0 0 0
1 2
2 1
3 1
1 3
Output
0 2 3 0
Submitted Solution:
```
[n, m, k] = [int(x) for x in input().split()]
membership = []
notification = [0] * n
for i in range(0, n):
membership.append([int(x) for x in input().split()])
for ii in range(0, k):
log = [int(x) for x in input().split()]
for j in range(0, n):
if membership[j][log[1]-1] == 1 and j != (log[0] - 1):
notification[j] += 1
print(notification)
``` | instruction | 0 | 39,952 | 14 | 79,904 |
No | output | 1 | 39,952 | 14 | 79,905 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.
R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.
The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.
Input
The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly.
Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.
Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.
Output
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
Examples
Input
3 4 5
1 1 1 1
1 0 1 1
1 1 0 0
1 1
3 1
1 3
2 4
3 2
Output
3 3 1
Input
4 3 4
0 1 1
1 0 1
1 1 1
0 0 0
1 2
2 1
3 1
1 3
Output
0 2 3 0
Submitted Solution:
```
n, m, k = map(int, input().split())
list_of_a = [list(map(int, input().split())) for i in range(n)]
list_of_xy = [list(map(int, input().split())) for i in range(k)]
list_of_n = [str(0)] * n
for sotrud in list_of_a:
for matriza in list_of_xy:
sot = list_of_a.index(sotrud)
if sotrud[matriza[1] - 1] == 1:
if matriza[0] != (sot + 1) :
list_of_n[sot] += str(int(list_of_n[sot]) + 1)
print(" ".join(list_of_n))
``` | instruction | 0 | 39,953 | 14 | 79,906 |
No | output | 1 | 39,953 | 14 | 79,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.
R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.
The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.
Input
The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly.
Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.
Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.
Output
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
Examples
Input
3 4 5
1 1 1 1
1 0 1 1
1 1 0 0
1 1
3 1
1 3
2 4
3 2
Output
3 3 1
Input
4 3 4
0 1 1
1 0 1
1 1 1
0 0 0
1 2
2 1
3 1
1 3
Output
0 2 3 0
Submitted Solution:
```
n,m,k=input().split()
n=int(n)
m=int(m)
k=int(k)
l=[]
for i in range(n):
l.append(list(map(int,input().split())))
t=[[0]*2]*k
e=[0]*n
c=[0]*m
for i in range(k):
t0,t1=map(int,input().split())
t[0][0]=t0
t[0][1]=t1
e[t0-1]-=1
c[t0-1]+=1
p=""
for i in range(n):
for j in range(m):
if(l[i][j]):
e[i]=e[i]+c[j]
if(e[i]<0):
p=p+"0 "
else:
p=p+str(e[i])+" "
print(p)
``` | instruction | 0 | 39,954 | 14 | 79,908 |
No | output | 1 | 39,954 | 14 | 79,909 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3 | instruction | 0 | 39,997 | 14 | 79,994 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
import math
n=int(input())
if n==1 or n==2:
print(1)
print(1)
elif n==3:
print(2)
print('1','3',end=" ")
elif n==4:
print(4)
print('3','1','4','2',end=" ")
else:
print(n)
lst=[]
k1=1
k2=math.ceil(n/2) + 1
for j in range(n):
if j%2==0:
lst.append(k1)
k1+=1
else:
lst.append(k2)
k2+=1
for j in range(n):
print(lst[j],end=" ")
``` | output | 1 | 39,997 | 14 | 79,995 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3 | instruction | 0 | 39,998 | 14 | 79,996 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
#!/usr/bin/env python3
n = int(input())
if n<3:
print(1)
print(1)
elif n==3:
print(2)
print('1 3')
elif n==4:
print(4)
print('2 4 1 3')
else:
print(n)
for i in range(1, n+1, 2):
print(i, end = ' ', flush = True)
for i in range(2, n+1, 2):
print(i, end = ' ', flush = True)
``` | output | 1 | 39,998 | 14 | 79,997 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3 | instruction | 0 | 39,999 | 14 | 79,998 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
n = int(input())
print((n, n - 1)[(n < 4) - (n == 1)])
print(*(range(n - 1, 0, -2),'')[n - 2 < 2], *(range(n, 0, -2)))
``` | output | 1 | 39,999 | 14 | 79,999 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3 | instruction | 0 | 40,000 | 14 | 80,000 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
x = int(input())
if x > 3:
print(x)
if x%2 == 1:
for i in range(x,0,-2):
print(i,end=" ")
for j in range(x-1,0,-2):
print(j,end=" ")
else:
for j in range(x-1,0,-2):
print(j,end=" ")
for i in range(x,0,-2):
print(i,end=" ")
if x <=3:
if x == 3:
print(2)
print(1,3)
elif x == 2:
print(1)
print(1)
else:
print(1)
print(1)
``` | output | 1 | 40,000 | 14 | 80,001 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3 | instruction | 0 | 40,001 | 14 | 80,002 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
# 534A
__author__ = 'artyom'
n = int(input())
a = [1] if n <= 2 else [1, 3] if n == 3 else [3, 1, 4, 2] if n == 4 else [i for i in range(1, n + 1, 2)] + [i for i in range(2, n + 1, 2)]
print(len(a))
print(' '.join(map(str, a)))
``` | output | 1 | 40,001 | 14 | 80,003 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3 | instruction | 0 | 40,002 | 14 | 80,004 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
x=int(input())
if x==1:
print(1)
print(1)
elif x == 2:
print(1)
print(1)
elif x == 3:
print(2)
print(1, 3)
else:
if x%2==0:
t=[0]*x
for i in range(x//2):
t[2*i+1] = i+1
for i in range(x//2):
t[2*i] = i+x//2+1
print(x)
print(' '.join([str(x) for x in t]))
else:
t=[0]*x
for i in range(x//2):
t[2*i+1] = i+1
for i in range(x//2):
t[2*i] = i+x//2+1
print(x)
t[-1]=x
print(' '.join([str(x) for x in t]))
``` | output | 1 | 40,002 | 14 | 80,005 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3 | instruction | 0 | 40,003 | 14 | 80,006 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
N=int(input())
if N<=2:
print('1\n1')
elif N==3:
print('2\n1 3')
elif N==4:
print('4\n2 4 1 3')
else:
print(N)
for I in range(1,N+1,2):
print(I)
for I in range(2,N+1,2):
print(I)
``` | output | 1 | 40,003 | 14 | 80,007 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3 | instruction | 0 | 40,004 | 14 | 80,008 |
Tags: constructive algorithms, implementation, math
Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-03-05 15:53:55
# @Author : Lweleth (SoungEarlf@gmail.com)
# @Link : https://github.com/
# @Version : $Id$
n = int(input())
if n < 5:
if n == 1 or n == 2:
print("1\n1")
elif n == 3:
print("2\n1 3")
elif n == 4:
print("4\n2 4 1 3 ")
exit(0)
print(n)
for i in range(1, int((n + 1) / 2)):
print(i, i + int((n + 1)/2), end=' ')
if n % 2 == 1:
print(int((n + 1) / 2))
else:
print(int(n / 2), n)
``` | output | 1 | 40,004 | 14 | 80,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3
Submitted Solution:
```
n = int(input())
if n == 1 or n == 2:
print(1)
print(1)
elif n == 3:
print(2)
print(1, 3)
elif n == 4:
print(4)
print(2, 4, 1, 3)
else:
print(n)
i = 1
while i <= n:
print(i, end=" ")
i += 2
i = 2
while i <= n:
print(i, end=" ")
i += 2
``` | instruction | 0 | 40,005 | 14 | 80,010 |
Yes | output | 1 | 40,005 | 14 | 80,011 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3
Submitted Solution:
```
n = int(input())
if n == 2:
print('1\n1')
elif n == 3:
print('2\n1 3')
elif n == 4:
print('4\n3 1 4 2')
else:
print(n)
for i in range(1, n+1, 2): print(i, end=' ')
for i in range(2, n+1, 2): print(i, end=' ')
``` | instruction | 0 | 40,006 | 14 | 80,012 |
Yes | output | 1 | 40,006 | 14 | 80,013 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3
Submitted Solution:
```
n=int(input())
if(n==2 or n==1):
print(1)
print(1)
elif(n==3):
print(2)
print('1 3')
else:
s1=''
s2=''
for i in range(1,n+1):
if(i%2==0):
s1+=str(i)+' '
else:
s2+=str(i)+' '
print(n)
print(s1+s2)
``` | instruction | 0 | 40,007 | 14 | 80,014 |
Yes | output | 1 | 40,007 | 14 | 80,015 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3
Submitted Solution:
```
n = int(input())
L = []
for i in range(1, n + 1):
L.append(i)
if(n == 1 or n == 2):
print("1")
print("1")
elif(n == 3):
print("2")
print("1" + " " + "3")
elif(n == 4):
print("4")
print("3" + " " + "1" + " " + "4" + " " + "2")
else:
odd = []
even = []
for i in L:
if(i & 1):
odd.append(i)
else:
even.append(i)
nums = odd + even
print(len(nums))
print(' '.join(map(str, nums)))
``` | instruction | 0 | 40,008 | 14 | 80,016 |
Yes | output | 1 | 40,008 | 14 | 80,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3
Submitted Solution:
```
n=int(input())
if n==1:
print(1)
print(1)
elif n==2:
print(1)
print(1)
elif n==3:
print(2)
print(3,1)
elif n==4:
print(4)
print(3,1,4,2)
elif n%4==0:
L=[]
for i in range(1,n//4+1):
L.append(i*4-1)
L.append(i*4-3)
L.append(i*4)
L.append(i*4-2)
print(n)
for e in L:
print(e,end=' ')
elif n%4==1:
L=[]
for i in range(1,n//4+1):
L.append(i*4-1)
L.append(i*4-3)
L.append(i*4)
L.append(i*4-2)
L.append(n)
print(n)
for e in L:
print(e,end=' ')
elif n%4==2:
L=[]
for i in range(1,n//4+1):
L.append(i*4-1)
L.append(i*4-3)
L.append(i*4)
L.append(i*4-2)
L.insert(0,n-1)
L.append(n)
print(n)
for e in L:
print(e,end=' ')
else :
L=[]
for i in range(1,n//4+1):
L.append(i*4-1)
L.append(i*4-3)
L.append(i*4)
L.append(i*4-2)
L.insert(0,n-2)
L.insert(2,n-1)
L.append(n)
print(n)
for e in L:
print(e,end=' ')
``` | instruction | 0 | 40,009 | 14 | 80,018 |
No | output | 1 | 40,009 | 14 | 80,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3
Submitted Solution:
```
n = int(input())
if n == 2:
print('1\n1')
elif n == 3:
print('2\n1 3')
elif n == 4:
print('3\n3 1 4 2')
else:
print(n)
for i in range(1, n+1, 2): print(i, end=' ')
for i in range(2, n+1, 2): print(i, end=' ')
``` | instruction | 0 | 40,010 | 14 | 80,020 |
No | output | 1 | 40,010 | 14 | 80,021 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3
Submitted Solution:
```
i = int(input())
if i == 2:
print(0)
elif i == 3:
print(2)
print("1 3")
elif i > 3:
impar = ""
par = ""
for x in range(1,i+1):
if x%2 == 0:
par += str(x) + " "
else:
impar += str(x) + " "
print(i)
print(par[:-1],impar[:-1])
``` | instruction | 0 | 40,011 | 14 | 80,022 |
No | output | 1 | 40,011 | 14 | 80,023 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
Input
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
Output
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1.
If there are several possible answers, output any of them.
Examples
Input
6
Output
6
1 5 3 6 2 4
Input
3
Output
2
1 3
Submitted Solution:
```
n=int(input())
if(n>3):
for i in range(n//2):
print(i+1,n-i,end=" ")
if(n%2!=0):
print(n+1)
else:
if(n==3):
print(1,3,end=" ")
if(n==2) or (n==1):
print(1)
``` | instruction | 0 | 40,012 | 14 | 80,024 |
No | output | 1 | 40,012 | 14 | 80,025 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6. | instruction | 0 | 40,135 | 14 | 80,270 |
Tags: *special, dp
Correct Solution:
```
n,m = map(int,input().split())
l = list(map(int,input().split()))
dp =[0]*n
dp[0] = min(m+1,n)
print(dp[0],end = " ")
for i in range(1,n):
if l[i] == 0:
v2 = min(n - 1, i + m)
v1 = max(0, i - m)
dp[i] = v2-v1+1
print(dp[i], end=" ")
continue
v2 = min(n-1,i+m)
v1 = max(0,i-m)
v3 = max(0,l[i]-1-m)
v4 = min(n-1,l[i]-1+m)
dp[i] = dp[l[i]-1]+(v2-v1+1)-max(0,min(v2,v4)-max(v1,v3)+1)
print(dp[i], end=" ")
``` | output | 1 | 40,135 | 14 | 80,271 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6. | instruction | 0 | 40,136 | 14 | 80,272 |
Tags: *special, dp
Correct Solution:
```
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[min(1+k,n)]*n
for i in range(1,n):
x = a[i] - 1
if x==-1:
b[i]=min(i,k)+1+min(n-i-1,k)
else:
b[i]=b[x]+min(k+1,i-(x+min(k,n-x-1)))+min(n-i-1,k)
print(*b)
``` | output | 1 | 40,136 | 14 | 80,273 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6. | instruction | 0 | 40,137 | 14 | 80,274 |
Tags: *special, dp
Correct Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = [min(i - 1, k) + 1 + min(n - i, k) for i in range(1, n + 1)]
# l + 1 + min(n - l, k) - (r - min(r - 1, k)) - число перекрываюихся сообщений
for i in range(1, n + 1):
a_ = a[i - 1]
if a_ != 0:
l, r = sorted([i, a_])
d1 = ans[a_ - 1]
d2 = max(0, l + 1 + min(n - l, k) - (r - min(r - 1, k)))
delta = d1 - d2
ans[i - 1] += delta
print(' '.join(map(str, ans)))
``` | output | 1 | 40,137 | 14 | 80,275 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6. | instruction | 0 | 40,138 | 14 | 80,276 |
Tags: *special, dp
Correct Solution:
```
n, k = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
dp = [0] * n
for i in range(n):
if a[i] == -1:
dp[i] = min(i, k) + min(n - i - 1, k) + 1
else:
dp[i] = dp[a[i]] - min(n - a[i] - 1, k) + min(n - i - 1, k) + min(2 * k, i - a[i] - 1) + 1
print(' '.join(list(map(str, dp))))
``` | output | 1 | 40,138 | 14 | 80,277 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6. | instruction | 0 | 40,139 | 14 | 80,278 |
Tags: *special, dp
Correct Solution:
```
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
ans = []
for i in range(n):
c = 0
imin = max(0, i - k)
imax = min(n-1, i + k)
if a[i] != 0:
c += ans[a[i]-1]
amin = max(0, a[i]-1 - k)
amax = min(n - 1, a[i]-1 + k)
if imin > amax:
c += imax-imin + 1
else:
c += imax-amax
else:
c += imax-imin + 1
ans.append(c)
print(" ".join([str(x) for x in ans]))
``` | output | 1 | 40,139 | 14 | 80,279 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6. | instruction | 0 | 40,140 | 14 | 80,280 |
Tags: *special, dp
Correct Solution:
```
def inter(s11,s12,s21,s22):
return max(0,min(s12,s22) - max(s11,s21) + 1)
n, k = map(int,input().split())
ai = list(map(int,input().split()))
anss = [0] * n
for i in range(n):
if ai[i] == 0:
s11 = 0
s12 = n-1
s21 = i-k
s22 = i+k
anss[i] = inter(s11,s12,s21,s22)
else:
num = ai[i]-1
s11 = num - k
s12 = num + k
s21 = i-k
s22 = i+k
temp = inter(s11,s12,s21,s22)
s11 = n
s12 = 2*n
s21 = i-k
s22 = i+k
temp2 = inter(s11,s12,s21,s22)
s11 = num - k
s12 = num + k
s21 = n
s22 = 2*n
temp3 = inter(s11,s12,s21,s22)
s11 = -n
s12 = 0
s21 = i-k
s22 = i+k
temp4 = inter(s11,s12,s21,s22)
anss[i] = anss[num] + 2 * k + 1 - temp - temp2 + temp3
print(" ".join(map(str,anss)))
``` | output | 1 | 40,140 | 14 | 80,281 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6. | instruction | 0 | 40,141 | 14 | 80,282 |
Tags: *special, dp
Correct Solution:
```
def get_border(i, n, k):
"""
Возвращает левую и правую границы (включительно)
для текущего id с проверкой выхода за пределы массива.
:param i: текущий id (ref - 1)
"""
left = i - k
if left < 0:
left = 0
right = i + k
if n <= right:
right = n - 1
return [left, right]
def add_border(borders, new_border):
"""
Добавляет текущие границы в общий список границ
при наличии пересечений - объединяет границы
"""
if not borders:
borders.append(new_border)
return
last_border = borders[-1]
left = last_border[0]
new_right = new_border[1]
new_left = new_border[0]
if left <= new_right:
last_border[0] = new_left
else:
borders.append(new_border)
def count(borders):
result = 0
for border in borders:
result += border[1] - border[0] + 1 # +1 т.к. границы включительно
return result
def read(idx):
borders = []
i = idx
border = get_border(idx, n, k)
while True:
if i < 0:
break
if results[i][0] != -1:
intersection = results[i][1] - borders[-1][0] + 1
if intersection < 0:
intersection = 0
result = results[i][0] + count(borders) - intersection
results[idx] = (result, border[1])
return result
add_border(borders, get_border(i, n, k))
i = refs[i] - 1
result = count(borders)
results[idx] = (result, border[1])
return result
n = int()
k = int()
refs = []
results = []
def main():
global n, k, refs, results
n, k = map(lambda s: int(s), input().split())
refs = list(map(lambda s: int(s), input().split()))
results = [(-1, -1) for i in range(n)]
for idx in range(n):
print(read(idx), end=' ')
print()
if __name__ == '__main__':
main()
``` | output | 1 | 40,141 | 14 | 80,283 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6. | instruction | 0 | 40,142 | 14 | 80,284 |
Tags: *special, dp
Correct Solution:
```
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
r = [0] * n
for i in range(min(k + 1, n)):
r[i] = 1 + i + (min(i + k, n - 1) - i)
for i in range(k + 1, n):
if a[i] == 0:
r[i] = 1 + k + (min(i + k, n - 1) - i)
else:
parent = a[i] - 1
r[i] = 1 + k + (min(i + k, n - 1) - i) + r[parent] - max(0, min(parent + k, n - 1) - (i - k) + 1)
print(*r)
``` | output | 1 | 40,142 | 14 | 80,285 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6.
Submitted Solution:
```
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
dp=[0 for _ in range(n)]
for i in range(0,n):
if a[i]==0 :
dp[i]=1+min(i,k)+min(n-1-i,k)
else:
if not ((a[i]-1+k) < i-k ):
# print(1 + min(i, k) + min(n-1 - i, k)+dp[a[i]-1])
# print((min(min(i+k,n-1),min(a[i]-1+k,n-1)),max(min(i-k,0),min(a[i]-1-k,0)),1))
dp[i] = 1 + min(i, k) + min(n -i-1, k)+dp[a[i]-1]- (min(min(i+k,n-1),min(a[i]-1+k,n-1))-max(max(i-k,0),max(a[i]-1-k,0))+1)
else:
dp[i] = 1 + min(i, k) + min(n - i-1, k) + dp[a[i] - 1]
print(*dp)
``` | instruction | 0 | 40,143 | 14 | 80,286 |
Yes | output | 1 | 40,143 | 14 | 80,287 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6.
Submitted Solution:
```
n, k = list(map(int, input().split()))
answer = [None] * n
for i, ref in enumerate(map(int, input().split())):
ref -= 1
max_cur_page = min(i + k, n - 1)
min_cur_page = max(i - k, 0)
answer[i] = max_cur_page - min_cur_page + 1
if ref >= 0:
max_ref_page = min(ref + k, n - 1)
answer[i] += answer[ref] - max(0, max_ref_page - min_cur_page + 1)
print(' '.join(map(str, answer)))
``` | instruction | 0 | 40,144 | 14 | 80,288 |
Yes | output | 1 | 40,144 | 14 | 80,289 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6.
Submitted Solution:
```
def intersection(a, b, k, l):
max_limit = min((a + k), l - 1)
min_limit = max((b - k), 0)
return max(max_limit - min_limit + 1, 0)
def left_k(i, k):
return k - max(k - i, 0)
def right_k(i, k, l):
return k - max(i + 1 + k - l, 0)
def window(messages, k):
amounts = []
for message, link in enumerate(messages):
link = link - 1
if link > -1:
inter = intersection(link, message, k, len(messages))
amount = amounts[link] + left_k(message, k) + right_k(message, k, len(messages)) + 1 - inter
else:
amount = left_k(message, k) + right_k(message, k, len(messages)) + 1
amounts.append(amount)
return amounts
l, k = input().split(" ")
messages = input().split(" ")
answer = window([int(m) for m in messages], int(k))
print(" ".join([str(a) for a in answer]))
``` | instruction | 0 | 40,145 | 14 | 80,290 |
Yes | output | 1 | 40,145 | 14 | 80,291 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6.
Submitted Solution:
```
def read():
return list(map(int,input().split()))
n,k=read()
a=read()
b=[min(k,n-1)+1]
for i in range(1,n):
if a[i]==0:
b.append(min(i,k)+1+min(n-i-1,k))
else:
b.append(b[a[i]-1]-min(k,n-a[i])+min(i-a[i],2*k)+min(n-i-1,k)+1)
print(*b)
``` | instruction | 0 | 40,146 | 14 | 80,292 |
Yes | output | 1 | 40,146 | 14 | 80,293 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6.
Submitted Solution:
```
(n, k) = map(int, input().split())
lst = []
for x in input().split():
lst.append(int(x) - 1)
array = []
for x in range(1, n + 1):
array.append([x])
for x in range(len(lst)):
temp = []
if lst[x] != -1:
array[x] = array[lst[x]][:]
for y in range(max(lst[x] + k + 1, x - k + 1), min(x + k + 2, n + 1)):
if not y in array[x]:
array[x].append(y)
print(len(array[x]), end = ' ')
``` | instruction | 0 | 40,147 | 14 | 80,294 |
No | output | 1 | 40,147 | 14 | 80,295 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6.
Submitted Solution:
```
n,k=map(int,input().split())
a=input().split()
for i in range(len(a)):
a[i]=int(a[i])
a[i]-=1
b=[]
if k>=n:
for i in range(n):
b.append(n)
else:
print(a)
b.append(min(n,k+1))
for i in range(1,n-k):
if a[i]!=-1:
if i-a[i]<=2*k:
b.append(b[a[i]]+2*k+1-(a[i]+k-i+k+1))
else:
b.append(b[a[i]]+2*k+1)
else:
b.append(2*k+1)
for i in range(n-k,n):
if a[i]!=-1:
if i-a[i]<2*k:
b.append(b[a[i]]+2*k+1-(a[i]+k-i+k+1)-(i+k-n))
else:
b.append(b[a[i]]+2*k+1-(i+k-n))
else:
b.append(2*k+1-(i+k-n+1))
print(b)
``` | instruction | 0 | 40,148 | 14 | 80,296 |
No | output | 1 | 40,148 | 14 | 80,297 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6.
Submitted Solution:
```
n,k=map(int,input().split())
m=[0]+list(map(int,input().split()))
dp=[0]*(n+1)
dp[1]=min(n,k+1)
print(dp[1],end=' ')
for i in range(2,n+1):
dp[i]=dp[m[i]]+(min(n,i+k)-max(i-k-1,m[i]+k) if i>m[i]+k else min(n,i+k)-min(m[i]+k,n))
print(dp[i],end=' ')
``` | instruction | 0 | 40,149 | 14 | 80,298 |
No | output | 1 | 40,149 | 14 | 80,299 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.
Output
Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.
Examples
Input
6 0
0 1 1 2 3 2
Output
1 2 2 3 3 3
Input
10 1
0 1 0 3 4 5 2 3 7 0
Output
2 3 3 4 5 6 6 6 8 2
Input
2 2
0 1
Output
2 2
Note
Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6.
Submitted Solution:
```
s = input().split()
n = int(s[0])
k = int(s[1])
a = [int(i) for i in input().split()]
if n < k+2:
print(str(n)+(' ' + str(n))*(n-1))
else:
twok = k*2+1
counts = []
for i in range(1, k+1):
counts.append(i + k)
for i in range(k+1, n-k):
if a[i-1] == 0:
counts.append(twok)
else:
p = i - a[i-1]
if p < twok:
counts.append(counts[a[i-1]-1]+p)
else:
counts.append(counts[a[i-1]-1]+twok)
for i in range(n-k, n+1):
if a[i-1] == 0:
counts.append(n-i+1+k)
elif n-k < a[i-1]:
counts.append(counts[a[i-1]-1])
else:
p = i - a[i-1]
if p < twok:
counts.append(counts[a[i-1]-1]+n-i-k+p)
else:
counts.append(counts[a[i-1]-1]+n-i+1+k)
print(counts[0], end='')
[print(' ' + str(counts[i]), end='') for i in range(1,n)]
``` | instruction | 0 | 40,150 | 14 | 80,300 |
No | output | 1 | 40,150 | 14 | 80,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image> | instruction | 0 | 40,151 | 14 | 80,302 |
Tags: data structures, dp, greedy
Correct Solution:
```
n = int(input())
m = list(map(int, input().split()))
num = [0 for _ in range(n)]
cur = 0
for i in range(n-1, -1, -1):
cur -= 1
alt = m[i]+1
if cur < alt:
cur = alt
j = i
while (j < n) and (num[j] < cur):
num[j] = cur
j += 1
else:
num[i] = cur
# print(*num)
print(sum(num)-n-sum(m))
``` | output | 1 | 40,151 | 14 | 80,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image> | instruction | 0 | 40,152 | 14 | 80,304 |
Tags: data structures, dp, greedy
Correct Solution:
```
n = int(input())
a = list(map(int , input().split()))
min_toptal = a.copy()
for i in range(1, n):
min_toptal[i] = max(min_toptal[i-1], a[i]+1)
min_toptal[0] = 1
for i in range(n-1, 0, -1):
min_toptal[i-1] = max(min_toptal[i]-1, min_toptal[i-1])
min_under = []
# print(*a)
# print(*min_toptal)
# print(*min_under)
underwater = sum((max(0, min_toptal[i] - a[i]- 1) for i in range(n)))
print(underwater)
``` | output | 1 | 40,152 | 14 | 80,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image> | instruction | 0 | 40,153 | 14 | 80,306 |
Tags: data structures, dp, greedy
Correct Solution:
```
n = int(input())
m = list(map(int, input().split()))
a = [0] * n
k = 0
for i in range(n):
k = max(k, m[i] + 1)
a[i] = k
for i in range(n - 1, 0, -1):
a[i - 1] = max(a[i] - 1, a[i - 1])
ans = 0
for i in range(n):
ans += a[i] - m[i] - 1
print(ans)
``` | output | 1 | 40,153 | 14 | 80,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image> | instruction | 0 | 40,154 | 14 | 80,308 |
Tags: data structures, dp, greedy
Correct Solution:
```
n = int(input())
above = list(map(int, input().split()))
total = [x + 1 for x in above]
# ensure at most increase by one
for i in range(0,n-1)[::-1]:
total[i] = max(total[i], total[i+1] - 1)
# ensure nondecreasing
for i in range(1,n):
total[i] = max(total[i], total[i-1])
below = [t - a - 1 for t, a in zip(total, above)]
#print("total =", total)
#print("above =", above)
#print("below =", below)
print(sum(below))
``` | output | 1 | 40,154 | 14 | 80,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image> | instruction | 0 | 40,155 | 14 | 80,310 |
Tags: data structures, dp, greedy
Correct Solution:
```
#!/usr/bin/env python3
from sys import stdin, stdout
def rint():
return map(int, stdin.readline().split())
#lines = stdin.readlines()
n = int(input())
u = list(rint())
u = [0] + u
mark = 0
b = [0]
for i in range(1,n+1):
uu = u[i]
b.append(i)
if uu >= mark:
inc = uu - mark + 1
l = len(b)
for i in range(inc):
b.pop()
mark += inc
tot = [1 for i in range(n+1)]
for bb in b:
tot[bb] = 0
for i in range(1, n+1):
tot[i] = tot[i-1] + tot[i]
ans = 0
for i in range(1, n+1):
ans += tot[i] - u[i] - 1
print(ans)
``` | output | 1 | 40,155 | 14 | 80,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image> | instruction | 0 | 40,156 | 14 | 80,312 |
Tags: data structures, dp, greedy
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
ma = [1] * n
for i in range(1, n):
ma[i] = max(ma[i - 1], a[i] + 1)
for i in range(n - 2, -1, -1):
ma[i] = max(ma[i + 1] - 1, ma[i])
print(sum(ma) - sum(a) - n)
``` | output | 1 | 40,156 | 14 | 80,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image> | instruction | 0 | 40,157 | 14 | 80,314 |
Tags: data structures, dp, greedy
Correct Solution:
```
n = int(input())
A = [int(x) for x in input().split()]
n = len(A)
LB = [0]*n
lvl = 0
for i in reversed(range(n)):
lvl = max(A[i],lvl)
LB[i] = lvl+1
lvl -= 1
#print(LB)
poss = [[1,1]]
for i in range(1,n):
l,h = poss[-1]
a = A[i]
l = max(a,l)
if a==h:
l += 1
poss.append([l,h+1])
"""
___ --- --- --- ---
___ === --- ---
___ ===
"""
Level = []
for i in range(n):
if Level:
Level.append(max(Level[-1],LB[i],poss[i][0]))
else:
Level.append(max(LB[i],poss[i][0]))
#print(LB)
#print(poss)
#print(Level)
count = 0
for i in range(n):
count += max(1,Level[i]-A[i])-1
print(count)
``` | output | 1 | 40,157 | 14 | 80,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
Input
6
0 1 0 3 0 2
Output
6
Input
5
0 1 2 1 2
Output
1
Input
5
0 1 1 2 2
Output
0
Note
In the first example, the following figure shows an optimal case.
<image>
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.
<image> | instruction | 0 | 40,158 | 14 | 80,316 |
Tags: data structures, dp, greedy
Correct Solution:
```
n = int(input())
a = list(map(int,input().strip().split()))
s = [0] * n
under = 0
for i in range(n):
s[i] = a[i]
for i in range(1,n):
s[i] = max(s[i],s[i-1])
for i in range(n-2,0,-1):
if s[i+1] - s[i] > 1:
s[i] = s[i+1] - 1
for i in range(n):
under += max(0,s[i] - a[i])
print(under)
``` | output | 1 | 40,158 | 14 | 80,317 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has come to a party as a special guest. There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.
Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0). A handshake will be performed as follows:
* Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).
* Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.
However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:
* Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).
What is the maximum possible happiness after M handshakes?
Constraints
* 1 \leq N \leq 10^5
* 1 \leq M \leq N^2
* 1 \leq A_i \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
A_1 A_2 ... A_N
Output
Print the maximum possible happiness after M handshakes.
Examples
Input
5 3
10 14 19 34 33
Output
202
Input
9 14
1 3 5 110 24 21 34 5 3
Output
1837
Input
9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076
Output
8128170 | instruction | 0 | 40,208 | 14 | 80,416 |
"Correct Solution:
```
from bisect import bisect_left, bisect_right
from itertools import accumulate
def ge_m(x, m, n):
total = 0
for v in a:
delta = x - v
idx = bisect_left(a, delta)
total += (n - idx)
return total >= m
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cum = [0] + list(accumulate(a)) # 累積和
# 1回の握手であがる幸福度のうち、最も低いものを求める
l = 0
r = a[-1] * 2 + 1
while r - l > 1:
mid = (l + r) // 2
if ge_m(mid, m, n):
l = mid
else:
r = mid
# 最も低い幸福度よりも大きい値を足す
num = 0 # 握手の回数
ans = 0
for v in a:
idx = bisect_right(a, l - v)
num += n - idx
ans += v * (n - idx) + (cum[-1] - cum[idx])
# 足りない握手の回数分、最も低い幸福度を足す
ans += (l * (m - num))
print(ans)
``` | output | 1 | 40,208 | 14 | 80,417 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has come to a party as a special guest. There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.
Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0). A handshake will be performed as follows:
* Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).
* Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.
However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:
* Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).
What is the maximum possible happiness after M handshakes?
Constraints
* 1 \leq N \leq 10^5
* 1 \leq M \leq N^2
* 1 \leq A_i \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
A_1 A_2 ... A_N
Output
Print the maximum possible happiness after M handshakes.
Examples
Input
5 3
10 14 19 34 33
Output
202
Input
9 14
1 3 5 110 24 21 34 5 3
Output
1837
Input
9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076
Output
8128170 | instruction | 0 | 40,209 | 14 | 80,418 |
"Correct Solution:
```
import sys
input=sys.stdin.readline
import bisect
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
r=a[n-1]*2+1
l=0
while r-l>=2:
mid=(l+r)//2
#mid以上はいくつあるか
#答えはl以上r未満
cnt=0
for i in range(n):
cnt+=n-bisect.bisect_left(a,mid-a[i])
if cnt>=k:
l=mid
else:
r=mid
#lだとk個以上 rだとk個未満
ans,p,=0,0
b=[0]
for i in range(n):
p+=a[i]
b.append(p)
for i in range(n):
q=bisect.bisect_left(a,r-a[i])
ans+=b[n]-b[q]
ans+=a[i]*(n-q)
k-=n-q
ans+=l*k
print(ans)
``` | output | 1 | 40,209 | 14 | 80,419 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.