Host by Github.

Power by Hexo.Theme by Simpleblock.License: CC BY-NC-SA 3.0.© 2018 Cosima Han.## 开始搞ACM

上个月开始搞ACM(ICPC)，虽然只是做一些水题。一道题苦想半天调试N遍最后AC，这感觉 特别爽哟。

随手拿最近的一些水题放上来吧，当做纪念，可能几年后我成为了大神，人们来嘲笑我说，这么烂的代码竟然是他曾经写的。

#### Codeforces Round #237 (Div. 2) - Valera and Fruits

Description

Valera loves his garden, where *n* fruit trees grow.

This year he will enjoy a great harvest! On the *i*-th tree *b*_{i} fruit grow, they will ripen on a day number *a*_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day *a*_{i} and day *a*_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat).

Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than*v* fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well?

Input

The first line contains two space-separated integers *n* and *v*(1 ≤ *n*, *v* ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day.

Next *n* lines contain the description of trees in the garden. The *i*-th line contains two space-separated integers *a*_{i} and *b*_{i}(1 ≤ *a*_{i}, *b*_{i} ≤ 3000) — the day the fruits ripen on the *i*-th tree and the number of fruits on the *i*-th tree.

Output

Print a single integer — the maximum number of fruit that Valera can collect.

Sample Input

2 3

1 5

2 3

8

5 10

3 20

2 20

1 20

4 20

5 20

60

Hint

In the first sample, in order to obtain the optimal answer, you should act as follows.

- On the first day collect 3 fruits from the 1-st tree.
- On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree.
- On the third day collect the remaining fruits from the 2-nd tree.

In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.

1 | #include<stdio.h> |

#### Codefoces 436B-Om Nom and Spiders

Description

Om Nom really likes candies and doesn’t like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn’t want to see them at all.

*n*×

*m*field. The park has

*k*spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.

Om Nom isn’t yet sure where to start his walk from but he definitely wants:

- to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders);
- to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park).

We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.

Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That’s why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.

Input

The first line contains three integers *n*, *m*, *k*(2 ≤ *n*, *m* ≤ 2000; 0 ≤ *k* ≤ *m*(*n* - 1)).

Each of the next *n* lines contains *m* characters — the description of the park. The characters in the *i*-th line describe the *i*-th row of the park field. If the character in the line equals “.“, that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: “L“ (meaning that this cell has a spider at time 0, moving left), “R“ (a spider moving right), “U“ (a spider moving up), “D“ (a spider moving down).

It is guaranteed that the first row doesn’t contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly *k* spiders.

Output

Print *m* integers: the *j*-th integer must show the number of spiders Om Nom will see if he starts his walk from the *j*-th cell of the first row. The cells in any row of the field are numbered from left to right.

Sample Input

3 3 4

…

R.L

R.U

0 2 2

2 2 2

..

RL

1 1

2 2 2

..

LR

0 0

3 4 8

….

RRLL

UUUU

1 3 3 1

2 2 2

..

UU

0 0

Hint

Consider the first sample. The notes below show how the spider arrangement changes on the field over time:

… … ..U …

R.L -> .*U -> L.R -> …

R.U .R. ..R …

Character “*“ represents a cell that contains two spiders at the same time.

- If Om Nom starts from the first cell of the first row, he won’t see any spiders.
- If he starts from the second cell, he will see two spiders at time 1.
- If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2.

1 | #include<iostream> |

#### Codeforces 435B Pasha Maximizes

Description

Pasha has a positive integer *a* without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.

Help Pasha count the maximum number he can get if he has the time to make at most *k* swaps.

Input

The single line contains two integers *a* and *k*(1 ≤ *a* ≤ 10^{18}; 0 ≤ *k* ≤ 100).

Output

Print the maximum number that Pasha can get if he makes at most *k* swaps.

Sample Input

1990 1

9190

300 0

300

1034 2

3104

9090000078001234 6

9907000008001234

1 | #include<iostream> |

#### codeforce 432B - Football Kit

Description

Consider a football tournament where *n* teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the *i*-th team has color *x*_{i} and the kit for away games of this team has color *y*_{i}(*x*_{i} ≠ *y*_{i}).

In the tournament, each team plays exactly one home game and exactly one away game with each other team (*n*(*n* - 1) games in total). The team, that plays the home game, traditionally plays in its home kit. The team that plays an away game plays in its away kit. However, if two teams has the kits of the same color, they cannot be distinguished. In this case the away team plays in its home kit.

Calculate how many games in the described tournament each team plays in its home kit and how many games it plays in its away kit.

Input

The first line contains a single integer *n*(2 ≤ *n* ≤ 10^{5}) — the number of teams. Next *n* lines contain the description of the teams. The *i*-th line contains two space-separated numbers *x*_{i}, *y*_{i}(1 ≤ *x*_{i}, *y*_{i} ≤ 10^{5}; *x*_{i} ≠ *y*_{i}) — the color numbers for the home and away kits of the*i*-th team.

Output

For each team, print on a single line two space-separated integers — the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input.

Sample Input

2

1 2

2 1

2 0

2 0

3

1 2

2 1

1 3

3 1

4 0

2 2

1 | #include<iostream> |

#### Codeforces 431B Shower Line

Description

Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks.

There is only one shower and there are multiple students who wish to have a shower in the morning. That’s why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.

Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the(2*i* - 1)-th man in the line (for the current moment) talks with the (2*i*)-th one.

Let’s look at this process in more detail. Let’s number the people from 1 to 5. Let’s assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn’t talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn’t talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.

We know that if students *i* and *j* talk, then the *i*-th student’s happiness increases by *g*_{ij} and the *j*-th student’s happiness increases by *g*_{ji}. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower.

Input

The input consists of five lines, each line contains five space-separated integers: the *j*-th number in the *i*-th line shows *g*_{ij} (0 ≤ *g*_{ij} ≤ 10^{5}). It is guaranteed that *g*_{ii} = 0 for all *i*.

Assume that the students are numbered from 1 to 5.

Output

Print a single integer — the maximum possible total happiness of the students.

Sample Input

0 0 0 0 9

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

7 0 0 0 0

32

0 43 21 18 2

3 0 21 11 65

5 2 0 1 4

54 62 12 0 99

87 64 81 33 0

620

1 | #include<iostream> |