2016 PSUT Coding Marathon
A. LCS (A)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Haneen is trying to solve an easy problem given by Dr. Ibrahim in the Algorithms course. The problem is to find the lexicographically smallest Longest Common Subsequence (LCS) between two strings. Unfortunately, she got many runtime errors. After tens of wrong submissions, she asked Master Hazem about the problem and he replied:

"C strings are terminated with the null character ‘\0’. When you want to store a string of length N, the size of the array should be at least N + 1."

Haneen didn’t get anything of what he said, and now, she is in need of your help! Given the length of the string Haneen has, determine the minimum size of an array needed to store this string.

Input

The input contains a single integer N (1 ≤ N ≤ 105), the length of the string.

Output

Print the minimum size of an array needed to store the string.

Examples
Input
3
Output
4
Input
7
Output
8
Input
64
Output
65

B. LCS (B)
time limit per test
0.25 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Although Haneen was able to solve the LCS problem, Dr. Ibrahim is suspicious about whether she really understands the LCS problem or not. He believes that she can't write the code on her own, but can only translate the LCS pseudo-code given in class into C++ code without really understanding how it works. Here is the pseudo-code Dr. Ibrahim gave in class:

function LCS (A[1..R], B[1..C])
DP = array(0..R, 0..C)
for i := 0..R
DP[i,0] = 0
for j := 0..C
DP[0,j] = 0
for i := 1..R
for j := 1..C
if A[i] = B[j]
DP[i,j] := DP[i-1,j-1] + 1
else
DP[i,j] := max(DP[i,j-1], DP[i-1,j])
return DP[R,C]

To verify that Haneen understands the LCS problem, Dr. Ibrahim asked her to solve the following problem:

After running the above LCS code on two strings A and B, the 2D array DP is filled with values. Given the 2D array DP, can you guess what A and B are? Any two strings A and B that will produce the given table and contain only lowercase English letters are acceptable.

Can you help Haneen solve this problem?

Input

The first line of input contains two integers R and C (1 ≤ R, C ≤ 25), the length of the strings A and B, respectively.

Each of the following R + 1 lines contains C + 1 integers, these lines represent the 2D array DP.

It's guaranteed that the given table was produced by running the algorithm on two strings that contain only lowercase English letters.

Output

Print string A on the first line and string B on the second line. Both strings should contain only lowercase English letters.

Example
Input
3 4
0 0 0 0 0
0 0 1 1 1
0 0 1 1 2
0 1 1 1 2
Output
abc
cadb

C. 1D Cafeteria (A)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After a long ACM training day, Master Hasan invited his trainees for a light dinner in the cafeteria. The cafeteria has N tables, each with a number of chairs around it. Before accepting the trainees into the cafeteria, Master Hasan wants to know how many unused chairs are there in the cafeteria. If there isn't enough chairs, he might invite them to a well-known Indian restaurant.

Input

The first line of input contains a single integer N (1 ≤ N ≤ 100), the number of tables in the cafeteria.

Each of the following N lines contains two integers describing a table, A and B (1 ≤ A ≤ B ≤ 100), where A represents the number of used chairs, and B represents the total number of chairs around that table.

Output

Print the number of unused chairs in the cafeteria.

Example
Input
3
1 3
4 4
5 8
Output
5

D. 1D Cafeteria (B)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In this cafeteria, the N tables are all ordered in one line, where table number 1 is the closest to the window and table number N is the closest to the door.

Each time a group of X people enter the cafeteria, one of the cafeteria staff escorts the guests to the table with the smallest number of chairs greater than or equal to X chairs among all available tables. If there’s more than one such table, the guests are escorted to the table that is closest to the window. If there isn't any suitable table available, the group will leave the cafeteria immediately. A table is considered available if no one sits around it.

Eyad likes to help others and also likes to prove that he has learned something from the training of Master Hasan. Therefore, he decided to write a program that helps the cafeteria staff choose the right table for a newly arriving group.

Given the log file of who entered and who left the cafeteria, find the table at which a given group should sit.

Input

The first line of input contains two integers N and Q (1 ≤ N, Q ≤ 105), the number of tables in the cafeteria and the number of events in the log file.

The second line contains N integers, each represents the size of a table (number of chairs around it). The tables are given in order from 1 to N. The size of each table is at least 1 and at most 105.

Each of the following Q lines describes an event in one of the following formats:

- in X: means a group of X (1 ≤ X ≤ 105) people entered the cafeteria.

- out T: means the group on table number T (1 ≤ T ≤ N) just left the cafeteria, the table is now available. It is guaranteed that a group was sitting on this table (input is valid).

Initially, all tables are empty, and the log lists the events in the same order they occurred (in chronological order).

Output

For each event of the first type, print the number of the table on which the coming group should sit. If for any event a group cannot be escorted to any table, print  - 1 for that event.

Example
Input
4 7
1 2 6 7
in 4
in 1
in 3
in 5
out 1
out 4
in 7
Output
3
1
4
-1
4

E. Accepted Passwords
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Islam is usually in a hurry. He often types his passwords incorrectly. He hates retyping his password several times whenever he tries to login, especially that his passwords are usually very long. He believes that websites should be tolerant with very long passwords. In other words, he believes that if a password is very long, and there is only one mistake in the password, the website should allow the user to login.

Your task is to check if an entered password should be accepted according to Islam, or not. The entered password will be accepted if it matches the user’s password, or if the user’s password length is at least 8 characters and the user made a mistake with only one character (either replaced it with a wrong character or dropped it).

Given the user’s password, and the entered password, determine if the entered password should be accepted according to Islam.

Input

The first line of input contains the user’s password.

The second line of input contains the entered password.

Both strings contain only lowercase and uppercase English letters.

The length of each string is at least 1 and at most 100.

Output

Print yes if the entered password should be accepted according to Islam, otherwise print no.

Examples
Input
AgentMahone
IslamIsMahone
Output
no
Input
ofmahone
ofmahome
Output
yes
Input
algorithms
algorthms
Output
yes
Input
Mahone
mahonE
Output
no

F. Mission in Amman (A)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You must have heard about Agent Mahone! Dr. Ibrahim hired him to catch the cheaters in the Algorithms course. N students cheated and failed this semester and they all want to know who Mahone is in order to take revenge!

Agent Mahone is planning to visit Amman this weekend. During his visit, there are M places where he might appear. The N students are trying to cover these places with their leader Hammouri, who has been looking for Mahone since two semesters already!

Hammouri will be commanding students to change their places according to the intel he receives. Each time he commands a student to change his position, he wants to know the number of places that are not covered by anyone.

Can you help these desperate students and their leader Hammouri by writing an efficient program that does the job?

Input

The first line of input contains three integers N, M and Q (2 ≤ N, M, Q ≤ 105), the number of students, the number of places, and the number of commands by Hammouri, respectively.

Students are numbered from 1 to N. Places are numbered from 1 to M.

The second line contains N integers, where the ith integer represents the location covered by the ith student initially.

Each of the following Q lines represents a command and contains two integers, A and B, where A (1 ≤ A ≤ N) is the number of a student and B (1 ≤ B ≤ M) is the number of a place. The command means student number A should go and cover place number B. It is guaranteed that B is different from the place currently covered by student A.

Changes are given in chronological order.

Output

After each command, print the number of uncovered places.

Example
Input
4 5 4
1 2 1 2
1 3
2 4
4 5
3 5
Output
2
1
1
2

G. Mission in Amman (B)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are K hours left before Agent Mahone leaves Amman! Hammouri doesn't like how things are going in the mission and he doesn't want to fail again. Some places have too many students covering them, while other places have only few students.

Whenever Hammouri commands a student to change his place, it takes the student exactly one hour to reach the new place. Hammouri waits until he is sure that the student has arrived to the new place before he issues a new command. Therefore, only one student can change his place in each hour.

Hammouri wants to command the students to change their places such that after K hours, the maximum number of students covering the same place is minimized.

Given the number of students covering each place, your task is to find the maximum number of students covering the same place after K hours, assuming that Hammouri correctly minimizes this number.

Input

The first line of input contains two integers M (2 ≤ M ≤ 105) and K (1 ≤ K ≤ 109), where M is the number of places and K is the number of hours left before Agent Mahone leaves Amman.

The second line contains M non-negative integers, each represents the number of students covering one of the places. Each place is covered by at most 109 students.

Output

Print the maximum number of students covering a place after K hours, assuming that Hammouri minimized this number as much as possible in the last K hours.

Examples
Input
5 4
3 4 1 4 9
Output
5
Input
2 1000000000
1000000000 4
Output
500000002
Input
5 3
2 2 2 2 1
Output
2

H. Queue (A)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After the data structures exam, students lined up in the cafeteria to have a drink and chat about how much they have enjoyed the exam and how good their professors are. Since it was late in the evening, the cashier has already closed the cash register and does not have any change with him.

The students are going to pay using Jordanian money notes, which are of the following types: 1, 5, 10, 20, 50.

Given how much each student has to pay, the set of notes he’s going to pay with, and the order in which the students arrive at the cashier, your task is to find out if the cashier will have enough change to return to each of the student when they arrive at the cashier.

Input

The first line of input contains a single integer N (1 ≤ N ≤ 105), the number of students in the queue.

Each of the following N lines describes a student and contains 6 integers, K, F1, F2, F3, F4, and F5, where K represents the amount of money the student has to pay, and Fi (0 ≤ Fi ≤ 100) represents the amount of the ith type of money this student is going to give to the cashier.

The students are given in order; the first student is in front of the cashier.

It is guaranteed that no student will pay any extra notes. In other words, after removing any note from the set the student is going to give to the cashier, the amount of money will be less than what is required to buy the drink.

Output

Print yes if the cashier will have enough change to return to each of the students when they arrive in the given order, otherwise print no.

Examples
Input
3
4 0 1 0 0 0
9 4 1 0 0 0
8 0 0 1 0 0
Output
no
Input
3
9 4 1 0 0 0
4 0 1 0 0 0
8 0 0 1 0 0
Output
yes

I. Queue (B)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Amarneh likes Artificial Intelligence and always reads about intelligent techniques. As he was waiting in the line, he thought that there must be an intelligent way to re-order the students, such that there will always be enough change with the cashier.

Given the same input data as in the previous problem, can you help Amarneh find any order for serving the students in which the cashier will always have enough change to return to the students?

Input

The first line of input contains a single integer N (1 ≤ N ≤ 105), the number of students in the queue.

Each of the following N lines describes a student and contains 6 integers, K, F1, F2, F3, F4, and F5, where K represents the amount of money the student has to pay, and Fi (0 ≤ Fi ≤ 100) represents the amount of the ith type of money this student is going to give to the cashier.

It is guaranteed that no student will pay any extra notes. In other words, after removing any note from the set the student is going to give to the cashier, the amount of money will be less than what is required to buy the drink.

Output

Print N integers on a single line that represents the order in which the students should line up, where the first integer represents the number of the student in the front of the queue. If such order doesn’t exist, print  - 1.

If there is more than one possible order, you can print any of them.

Examples
Input
3
4 0 1 0 0 0
9 4 1 0 0 0
8 0 0 1 0 0
Output
2 1 3 
Input
1
40 0 0 0 0 1
Output
-1

J. Objects Panel (A)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Raihan is helping Maram and Master Hasan in the design of their graduation project. They have a panel that shows a list of the objects created by the user. Objects can be nested inside each other, the user can click the '-' sign to collapse the items nested inside an object, or click the '+' sign to expand the objects nested inside it.

Check the following table for more details:

Note that all objects are nested inside a list item called project.

For each object in the project, you are given a list of objects nested inside it and whether it’s expanded or collapsed. Can you help Raihan in drawing the panel?

Input

The first line of input contains an integer N (0 ≤ N ≤ 100), the number of objects in the project. Objects are numbered from 1 to N. The project item is item number 0.

Each of the following N + 1 lines contains the description of an object. The ith (starting from 0) line describes the object number i in the following format:

S K A1 A2 ... AK

Where S is the state of the object (expanded or collapsed), where '-' means it's expanded and '+' means it's collapsed. The state of objects that do not have any nested objects will be '-'.

K is the number of objects nested inside this object, this number is followed by K distinct numbers representing the numbers of the nested objects.

Output

Output the current state of the list, the items nested in an object should be listed in the given order.

An object inside another is nested by two spaces, the first space is replaced with '+' or '-' depending on the state of the object, if the object does not have other objects inside it, then keep the space.

Examples
Input
6
- 3 2 5 1
+ 1 3
- 1 6
- 1 4
- 0
- 0
- 0
Output
- project
- object2
object6
object5
+ object1
Input
0
- 0
Output
  project

K. Objects Panel (B)
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Since the list has a fixed height, Raihan had to implement a vertical scrollbar. In order for the scrollbar to work properly, the actual height of the items must be provided after every change in the state.

A change in the state means expanding or collapsing the items nested inside an object that is visible in the list.

Your task is to calculate the number of rows in the panel after each of Q given changes.

Input

The first line of input contains two integers N (1 ≤ N ≤ 3x105) and Q (1 ≤ Q ≤ 5x105), the number of objects and the number of changes, respectively.

The second line of input contains N integers, the ith (starting from 1) integer is the number of the parent object of object i. Note that the parent can be 0, and this means it is nested directly inside project.

The third line of input contains Q integers, each is an object number (from 0 to N) and represents a change in the state of an item, that is, a click over the sign beside that object.

Initially, all objects are expanded.

It is guaranteed that changes do not include hidden objects (objects that are not visible in the panel at the time of the change), or objects that don’t have any nested objects inside them.

Output

The output should contain Q integers on a single line, the ith of them is the number of rows in the panel after toggling the state of the ith object.

Examples
Input
5 5
3 3 0 1 3
1 1 0 0 3
Output
5 6 1 6 2 
Input
1 4
0
0 0 0 0
Output
1 2 1 2 

L. Plus or Minus (A)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tries to make it correct as quickly as possible!

Given an equation of the form: A1 o A2 o A3 o ... o An  =  0, where o is either + or -. Your task is to help AbdelKader find the minimum number of changes to the operators + and -, such that the equation becomes correct.

You are allowed to replace any number of pluses with minuses, and any number of minuses with pluses.

Input

The first line of input contains an integer N (2 ≤ N ≤ 20), the number of terms in the equation.

The second line contains N integers separated by a plus + or a minus -, each value is between 1 and 108.

Values and operators are separated by a single space.

Output

If it is impossible to make the equation correct by replacing operators, print  - 1, otherwise print the minimum number of needed changes.

Examples
Input
7
1 + 1 - 4 - 4 - 4 - 2 - 2
Output
3
Input
3
5 + 3 - 7
Output
-1

M. Plus or Minus (B)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

George met AbdelKader in the corridor of the CS department busy trying to fix a group of incorrect equations. Seeing how fast he is, George decided to challenge AbdelKader with a very large incorrect equation. AbdelKader happily accepted the challenge!

Input

The first line of input contains an integer N (2 ≤ N ≤ 300), the number of terms in the equation.

The second line contains N integers separated by a plus + or a minus -, each value is between 1 and 300.

Values and operators are separated by a single space.

Output

If it is impossible to make the equation correct by replacing operators, print  - 1, otherwise print the minimum number of needed changes.

Examples
Input
7
1 + 1 - 4 - 4 - 4 - 2 - 2
Output
3
Input
3
5 + 3 - 7
Output
-1

N. Mixed Dimensions
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mixed Dimensions is an innovative company that is specialized in 3D printing technologies. They are planning to 3D print decorative tiles to be used in the beautiful pavements of Amman.

After printing thousands of these decorative tiles, Mixed Dimensions washed the tiles with water to make them look fresh before they are delivered. Unfortunately, water gathered inside some places in the decorations. Observing the problem, they decided to fill any cavity (hole) in the decorations so that no water gathers after a rainy day.

Given a 3D printed tile that represents a grid of size R×C, and the height in unit cubes above each point in the grid, your task is to compute the volume of printing material needed to fill the cavities (holes).

Two units3 of printing material are needed to fill this tile.

Input

The first line of input contains two integers R and C (1 ≤ R, C ≤ 500), the number of rows and columns of the base grid, respectively.

Each of the following R lines contains C integers, each integer is between 1 and 109 and represents the height in unit cubes above one of the cells.

Output

Print the volume of the printing material needed to fill the given tile so that no water gathers after a rainy day.

Examples
Input
4 3
2 3 3
2 1 2
3 1 3
3 2 1
Output
2