The first line contains two integers n and m (1 ≤ n, m ≤ 20) — the sizes of the table. Next n lines each contains m characters — the description of the table. The description means the following:   character "B" is a cell with a bomb;  character "S" is the starting cell, you can assume that it's empty;  digit c (1-8) is treasure with index c;  character "." is an empty cell;  character "#" is an obstacle.  Assume that the map has t treasures. Next t lines contain the prices of the treasures. The i-th line contains the price of the treasure with index i, vi ( - 200 ≤ vi ≤ 200). It is guaranteed that the treasures are numbered from 1 to t. It is guaranteed that the map has not more than 8 objects in total. Objects are bombs and treasures. It is guaranteed that the map has exactly one character "S".

NoteIn the first example the answer will look as follows.  In the second example the answer will look as follows.  In the third example you cannot get profit.In the fourth example you cannot get profit as you cannot construct a closed path with more than one cell.

Print a single integer — the maximum possible profit you can get.

You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure with a certain price, or a bomb, or an empty cell. Your initial position is also given to you.You can go from one cell of the map to a side-adjacent one. At that, you are not allowed to go beyond the borders of the map, enter the cells with treasures, obstacles and bombs. To pick the treasures, you need to build a closed path (starting and ending in the starting cell). The closed path mustn't contain any cells with bombs inside. Let's assume that the sum of the treasures' values that are located inside the closed path equals v, and besides, you've made k single moves (from one cell to another) while you were going through the path, then such path brings you the profit of v - k rubles.Your task is to build a closed path that doesn't contain any bombs and brings maximum profit.Note that the path can have self-intersections. In order to determine if a cell lies inside a path or not, use the following algorithm:  Assume that the table cells are points on the plane (the table cell on the intersection of the i-th column and the j-th row is point (i, j)). And the given path is a closed polyline that goes through these points.  You need to find out if the point p of the table that is not crossed by the polyline lies inside the polyline.  Let's draw a ray that starts from point p and does not intersect other points of the table (such ray must exist).  Let's count the number of segments of the polyline that intersect the painted ray. If this number is odd, we assume that point p (and consequently, the table cell) lie inside the polyline (path). Otherwise, we assume that it lies outside.

Input:
4 4
....
.S1.
....
....
10

Output: 2

Input:
7 7
.......
.1###2.
.#...#.
.#.B.#.
.3...4.
..##...
......S
100
100
100
100

Output: 364

Input:
7 8
........
........
....1B..
.S......
....2...
3.......
........
100
-100
100

Output: 0

Input:
1 1
S

Output: 0

Input:
10 11
..#...#.#.#
#..##.#####
7#.#5.#####
..##.##.###
#.3##.##...
###.#.##1.#
.#....#..##
.###.######
##42.#S#..#
...####.#.6
-31
30
-6
-12
-30
1
55

Output: 0

Input:
10 11
.....#1..##
..#3..#S#.#
........#..
..##...7...
....#.#.#..
.#...#.#.65
.#...##.#2.
......#..##
#.4.......#
.#...#.#...
0
-36
1
56
57
-31
-16

Output: 0

Input:
10 11
........S..
...........
5........1.
.7#........
...64.#....
...........
..........#
.2.........
.....3.....
...........
-9
33
9
-20
12
10
-29

Output: 8

Input:
10 11
#....1#....
.#.5.#.3S#.
###.##...##
###.#......
.#.#..#.#..
#..2.#.####
...#..#7##.
#4.##..#.6.
.#......#..
...#..##...
55
-13
44
46
22
-26
56

Output: 0

Input:
10 11
#......#..4
#.#..##....
##.....##.#
#1..#....##
..#.#.#.#.#
##..#6.#.##
#7#...2..#5
..#.#.#....
.#..S.#3.#.
..###.##.##
-33
-39
58
20
33
54
-25

Output: 0

Input:
10 11
.....##.##.
#.#.#.S..#.
....#....##
.3.##.2.7..
.#..#....4#
#.####..#.#
...#....5.#
.6.##...#.#
..##..#..##
.#1##..#.##
38
-37
-2
-27
29
47
-14

Output: 0

Input:
10 11
..##.#..#.1
##.##..#...
..###.#..#.
#...5#.3.##
##6#.#...#.
S.##4#...##
###.#......
.#..#..#...
..#..#7.###
#.....#.2..
11
5
59
19
1
-23
-11

Output: 0

Input:
10 11
###..#..##7
..#.#.4#.##
......5...#
.#.1#......
.#.#.......
.#....#.#..
##.######62
##...#3....
.#.#..#..##
S....##..#.
-6
-23
-15
12
40
-3
30

Output: 0

Input:
10 11
...........
...........
.#.B.......
......1#...
...........
....B......
...B...#...
.......#.2.
....B......
...#.....#S
50
-1

Output: 26

Input:
10 11
.3.....#...
...........
....6#.....
#..........
...#.7.....
.S.......5.
........4..
#...#..2...
...........
..........1
2
56
45
-34
29
26
-17

Output: 41

Input:
10 11
...........
.....#.#..#
...#.4#....
##.1.......
#....3.##.#
..6....#...
#.##...#.S.
.#......#7.
....5.#2...
...#.......
9
28
56
-14
32
-12
15

Output: 76

Input:
10 11
###.#4.#.#.
#.....#.#.#
.#..#####..
#.#..#6.##.
#..23##..##
##..##....#
#..##....7.
#.###.#.#.#
#.S##5#..#1
###.#..##..
4
15
30
-18
-30
38
40

Output: 0

Input:
10 11
1#.7###....
###.#####5#
#.#.#.####.
.###..###..
.#3#...#.#.
.###4##....
##.#..#.2..
#..#.##6#..
.#.########
##.S...###.
34
57
1
53
-24
11
-38

Output: 0

Input:
10 11
.#.3.#.....
.........1.
.......5#..
#..#..##...
...#.....4.
2..........
....6....7.
......#....
.........#.
...S.......
49
-37
58
-17
33
9
-17

Output: 59

Input:
10 11
##....##2#.
##...#.###.
...#.......
...#.#.#.#.
.#.###..#5.
#..#S#.#...
#3#.7##.###
#.#.####.#.
#.###.64#1.
.#.#..#.##.
-33
52
-4
-32
-3
29
-27

Output: 0

Input:
10 11
...#...##.#
........##.
5.#..#.S.#.
#..........
.....#..#.7
6...4......
#..3###2.#.
.#...##.##.
.#.#...1...
.....#.#...
-34
38
1
-8
58
50
50

Output: 0

Input:
10 11
##.#.###.#.
.1###.#.5##
##.....#.##
#.#.#######
#4#7##.##.#
.###.###.#.
..#S.#.####
..2###.####
.....##..6#
.3##..###..
50
-21
-6
5
29
56
42

Output: 0

Input:
10 11
62.#.#.1.#.
#####..##..
..#.##.#.##
####.#..#.S
7.#.#.#####
....##.##.#
####.##.#.#
.#...##.###
..#3....#..
.....5.#..4
6
40
-21
14
19
5
1

Output: 0

Input:
10 11
..#.##.1...
##.#..##6#.
..#.#..#..4
.....S.#.#.
#####.#.##.
7#####.5###
#3..##.....
#..#.#...2.
.###...##.#
.#....#.#.#
-18
1
-8
-7
-27
13
15

Output: 0

Input:
20 20
....................
....................
....................
....................
....................
.........6..........
..........5.........
...................8
....................
....................
...............7....
....................
....3...............
..............4.....
2...................
....................
....1...............
....................
....................
S...................
1
200
30
20
34
-2
15
100

Output: 36

Input:
10 11
.#.1....#..
#.........#
4..##..5..#
6.##......#
.3...#.7...
....#......
##...#.....
..#S.#.##.#
.#......#..
#.....#.2#.
34
41
47
-7
19
36
47

Output: 42

Input:
20 20
..#.....#..#........
....##.##..#..#.#...
....#.#...#..#.#.#..
.#.........#........
........#.#.#.#..#.#
.....#...6..........
.#.#.#....5.....#...
.#..#.##...#....##.8
...##.#......#.....#
.#.##.......#.....#.
..#......##....7.#.#
....#...#...#...#.#.
..#.3...............
...##.#.#.....4#...#
2...................
.##....#......#.....
##..1..........#....
......##....##....##
..#............#..#.
S.................#.
30
80
70
60
34
-22
15
100

Output: 106

Input:
20 20
#..#.....#.#.#......
...#...##.7.........
........#..#.......#
.......#.....#.....#
.......#......#.....
..#.........#......#
...#.#..#.#...#....#
...##....6...#...1..
.............#.....S
.......5.....#......
......#........#.#..
.....#..#..#...#....
..#......#.##.......
.#..................
#...................
.....#.#....8...3...
#....4#..#..........
....#.....##.....#.#
##2..........#......
#.#..#..#......#....
86
-14
65
54
27
53
33
63

Output: 288

Input:
20 20
.#....#...###..##...
#...#..####.#.#...#.
.#.#...###.##.......
##....#.##...##...##
...#.##........#4..#
....###.....##...#.#
..#.#..#.##..###....
.........8....#.....
.##..#..#.####..#.#.
.##.#......##..##...
..####..#..#...#...5
...#....#..##.....##
.#.....#....7....#.#
#S....###...###..##.
#..#.#.#..#6..##.#..
2..........#..##..#1
..#...###....####..#
........#.#3...##...
##.####...#...#.#...
.......#....#.#.....
-16
93
-23
7
12
85
51
10

Output: 0

Input:
20 20
#..#....#.#....###..
#.....#.....##....##
#.###........##.....
#..####.#.##...#.#..
.##....#.....#5...#.
.....#####1.###.#..#
#.###...#..##.6.....
#4.#.#.....8..#.####
##.#....#.....3#.##.
#..##........##.....
.#.#..####2......#..
.#.#......##.##.#...
#....##.#.###..#...#
.#.#.##.#..##.#.#...
....#.##..#..#.##.##
.#.......7###....#.#
..##.##....#.##....#
####....#...#..#.#..
#.#.#.#.##.....###.#
####........###.#S#.
0
78
27
78
-25
47
-19
84

Output: 0

Input:
20 20
...##...#..#...3.#..
.......##4#.##.#.##.
...#....#....#....#.
.##...#.#.##.###.#..
.##......5...#....##
.#..#.#.#.#.......1.
##.##..#.##.#..#...S
....#.....#.##......
...2...#........#..#
..8#....#.##...#.#..
.....7.#..#.....#...
.#...##......#.#.6..
......#...###..#...#
....#..#............
......#..#.#....#...
#....##.#....#......
.##.......###.#.##.#
..#.....####.#..#...
#...#...#..##.....#.
.......#.#.#..#.#.#.
58
83
96
66
69
60
7
10

Output: 96

Input:
20 20
....###.......#.#...
.....##......#.#....
##.##........##.....
.#..........#....#..
#......#..#.....##..
##.#......#...#.....
5.#.#..##.......2...
#....###..#.....#...
....#.#....#......#.
.#....#......#....#.
...S#......#........
3...#..#.##.6#..#..#
.......##.7....##...
#...#...#.##..#..#8#
........###..#......
....#..#....#...#...
#.#..#..#.......4...
#.#..#.......#...1..
.#...#.#..#.###.##..
#....##.......##....
55
-12
-18
46
43
78
52
82

Output: 90

Input:
20 20
.....#........#.....
...#.2...1..........
...........#........
..#.................
..#............7...#
..#...#.............
..................#.
#...................
....................
..#.....#.....#.....
.S......#......#....
....................
#.#...45.....8......
....................
....................
.........#........#.
.......6...#........
....#..#............
.#..............3...
...........#........
69
17
65
58
46
-14
82
84

Output: 334

Input:
4 4
....
.S1.
...#
....
10

Output: 0

Input:
20 20
..........#.....1...
...........#.#......
........#.......S.#.
.....2.....#...#.3..
...#......4.........
..6.................
...........#.....8..
................#...
....................
....................
....................
......#.............
....................
..5.................
7................#..
....................
..............#.....
..........#.........
....................
.....#.........#....
57
4
49
22
32
-7
-2
57

Output: 100

Input:
20 20
.#.......#..#.#....#
...#........#.#..#..
#.##....##.#.....#3.
.#..#.#....#......##
..###......#.....#..
..#..#..1#.6........
..###...#.....#...##
.......#..##...#....
........#...#..##...
.###..#.....2..#....
#.....#.#.###...#...
.....#.#.###........
....#.#...#.........
.......##...4.#.....
..#8.............#..
#..#...#...S#.......
.........#.#........
....7##......#.##...
#.............#.#...
#...#.5.#...........
63
94
82
96
44
38
79
16

Output: 296

Input:
20 20
..4..#.....#.3#.##..
##.......#..........
.#........#..#......
.#........#..S...6#.
..#.#72...........#.
..........##.####.##
......#.............
###.#......#.#......
##.....#.##..#...##.
..##...#..........#.
...8#...#....##...#.
...#..#.....#.....#.
.#...#..#..#.....#.#
.#.##.......##.##...
.###....#.#....#.#..
#.....#......#..5..#
.1..#.....#.#.#...#.
.#............#.#...
....#...#.....##.#.#
#.#....#.....#......
4
62
87
30
90
79
91
28

Output: 127

Input:
20 20
...#........##......
......#......#...#..
##..................
......6....#........
..#.................
.7........##.....#..
....5.....#....#....
.#........#.......#.
.............#......
....................
.#...............8..
..#.........4..#....
....#.........#.....
...........#....S...
.........#..........
........#.........#.
..................##
.##.....#.#3..1....2
.....#....#...#..#..
..........#.........
13
96
50
87
36
-20
22
50

Output: 140

Input:
20 20
......#...##........
......#..#.#.##.##.#
.....##.##.#.##.###.
.#...#.#.##...#....#
...3#.#.#..#..4..#..
...#.........##..#..
.....#.##..#........
.......#..#.#....#..
......#.##......#.#.
#..#...#....###.S...
....#...........##..
.....#.#..#.....#...
#..#....#..##.#...5#
.##......#.#..#.#..#
.....#....#.....#7.#
..#......#...###.#..
...##...2#.1#..#.#..
.###...........#...#
..#..##...#8........
.....#.#.....6..###.
-13
40
37
36
52
62
52
-22

Output: 51

Input:
20 20
........#...........
...#.......#.#.....#
....................
.#........#.........
....................
..#....#.3..........
.....#..#......#....
....#..............1
.#.........#.....##.
#2..................
.............#......
..#.................
......8..........##.
...........7........
...................4
........#.........5S
...........#........
.....#...........#..
..................6.
....................
25
-21
-2
27
49
-23
6
45

Output: 11

Input:
20 20
.#................#.
.#...#####.#..#.##.S
5#....#..#.#.#.##..#
.#.....##.#.#...#..#
......####.##.##.#..
##....#....#........
..#...#........#...#
.#..#.###..#.##.##.#
.#4...##........#.#.
..#.........#.......
....##.#.....#.###..
.8..#....#.#........
#..#....###....###3#
#...#.#.###..##....7
...#..........##.#..
...6.#.........#....
..#.##.##.....#..##.
.......#........##1.
.#....###......#...#
......#....##2#.....
16
33
-14
4
-5
-16
66
54

Output: 0

Input:
20 20
.#.##.##...#.####...
.#.#.......#......#.
#.#...#.....#......#
.##.8.##.#....##..##
##..#..#.#.#....#.##
##.#.#####.#.#.#...#
.....4.###...###.#.#
##.####.##.#.#...#..
.#..#.##..#.#..#....
..#.#.##...##..#....
#..##..#..#.....#.##
...#.##....#####...#
...6#5..#....#......
###...#.#.#..##.....
#..#....##.##.###..#
..#.##...##..#######
.#.##.2#.3.....#..##
#....#.#7#.##.#....S
##.#.......#...#.#..
..##.####1.#####.#..
16
67
-19
15
35
55
30
-23

Output: 0

Input:
20 20
....................
....................
.............6......
#........5..........
....................
....................
....................
......3.............
.......1............
...............#....
.............4......
....................
....................
....................
....................
....7...............
......2...8.........
...S................
....................
....................
33
55
19
29
29
44
31
-7

Output: 186

Input:
20 20
#...4....#..#...#...
..#.....#..#.......#
##.##.##.###.#....5.
####...#.#.#.....#..
......#.#.#...#.....
#...#.###..#....#...
..#..#......#...#...
#.....#.....#.####..
...6..#.2....##.....
#.####.#......1#..#.
..#.#...#..#..#.##..
#....#.....#..#.....
##..##.#............
......###.....###..3
.#.#.......7..#.#..#
..###.#..8........#.
#.##...........##...
#..#....#.#.##...#.S
#.............#.....
.#........###......#
57
28
-26
69
58
-13
54
45

Output: 67

Input:
5 5
.....
.S1..
.2...
...#.
.....
10
5

Output: 3

Input:
20 20
....#......#S.####2#
#........#....##..#.
.....#...#.######...
.#.#..##.#..##.#...#
.......#..#....#.#.#
..##........###.##.#
##.##.#.#....###.###
###3#..#.#....##7#..
##.......#......#...
#..#...4....#...###.
#####.##...#.##.#.#.
###.#......####.##..
.#.#.#..###...#.#..#
#...#.....6...###..#
.###.##.##.##......#
.#..#..#.#...###..#.
..#......##....#....
..5#.#....#.##.#...#
..#....#..########.#
##8.##1#..#....#..##
24
55
6
69
18
18
-8
69

Output: 29

Input:
20 20
....#......#S.####2#
#........#....##..#.
.....#...#.######...
.#.#..##.#..##.#...#
.......#..#....#.#.#
..##........###.##.#
##.##.#.#....###.###
###3#..#.#....##7#..
##.......#......#...
#..#...4....#...###.
#####.##...#.##.#.#.
###.#......####.##..
.#.#.#..###...#.#..#
#...#.....6...###..#
.###.##.##.##......#
.#..#..#.#...###..#.
..#......##....#....
..5#.#....#.##.#...#
..#....#..########.#
##8.##1#..#....#..##
24
55
6
69
18
18
-8
69

Output: 29

Input:
20 20
....................
....................
....................
....................
.......3............
....................
.......B............
....................
.......1............
....................
...SB..B............
....................
.......2............
....................
.......B............
....................
.......4............
....................
....................
....................
50
50
50
50

Output: 148

Input:
12 12
............
......B.....
........1...
............
.......4....
..B#..S.....
............
............
.2...#......
.....B......
B..........#
.......3....
5
-5
33
-9

Output: 0

Input:
12 12
............
............
..B........B
............
.........2..
...4........
....3....B..
..1B........
............
.S..........
............
............
16
-3
18
23

Output: 0

Input:
12 12
.....#......
.....##B...#
.........3..
......#.....
...........#
.....B.....S
...#2.......
...........1
............
..........4.
............
...B.....B..
25
9
-3
4

Output: 0

Input:
12 12
...#........
..B....B.#..
.......#....
.....B......
............
............
...#1.....#.
B.4...3..2#.
...#..#.....
..#..#......
......#..S..
..........#.
-2
23
15
-5

Output: 12

Input:
12 12
...#........
..B.B.......
.....#.....#
............
.......1....
......3.....
.....2......
............
...B.4......
........B...
....#.#.S...
............
-1
2
27
-7

Output: 4

Input:
12 12
...........#
.#..#...B.#.
1...........
3........#..
.....B......
#.........#B
....B2......
.#.....#....
............
4S.........#
...##.#...#.
.....##..#..
37
39
1
32

Output: 0

Input:
12 12
............
..#S#..B.#..
........##..
...#.......#
.1..#...B...
............
............
2...B...#...
.#........B.
.......#....
...#..#..#..
.3......#.4.
13
-6
3
33

Output: 0

Input:
10 10
..........
..........
..........
.....2....
...S...B..
..........
.3..B1.#..
.......#..
.5.....4..
...#.....#
100
10
14
11
20

Output: 18

Input:
12 12
.#..........
#...B.......
#...#.......
............
B...........
.3..#B...#..
..2#.#......
#......#4...
...BS.#.....
......#.....
...........1
............
-4
-6
6
10

Output: 0

Input:
12 12
............
.....3......
####.BB.....
.....#......
..#...B.2...
4...#.B....#
...#..#.....
.....#1#S...
.#..#.......
.....##....#
............
............
9
30
18
35

Output: 18

Input:
12 12
4.........#.
...........#
..S.......B.
..........#.
.....#.#....
.....B...#..
............
....#......#
............
.....3..B...
....B....1..
...2........
20
30
29
17

Output: 0

Input:
8 8
...#.#..
.....1..
..#3..##
.S#....#
.BB..#..
....B#..
...2.4..
.....#B.
44
16
30
0

Output: 0

Input:
8 8
B.......
.#1S...B
B....B..
....#.#.
.#......
...4....
#.2##3..
........
32
24
-8
37

Output: 31

Input:
8 8
..#.#...
.2..B..#
.B.S....
.B#...1.
#......B
......4.
#.3#.##.
.#.#...#
34
24
5
29

Output: 0

Input:
8 8
..#.B#..
..1.4...
...3...#
.B..B#..
#..B2...
#S......
.#.#..#.
.......#
49
-8
-4
28

Output: 0

Input:
8 8
B......S
...B..2.
...B4.3.
........
........
.1......
........
...B....
26
-6
15
45

Output: 9

Input:
8 8
#.#....#
....1...
..#.....
..#..S#.
.2......
...4..3.
...#.BB.
.#.B..#B
6
41
49
30

Output: 27

Input:
8 8
..#.....
...#....
.4....1#
..3.....
#B....B.
......B.
.#..B.2.
##.S#...
36
9
10
-6

Output: 0

Input:
10 10
..........
...6##....
.##..#....
.....2....
...S......
..........
.3..B1.#..
.......#..
.5.B...4..
...#.....#
100
10
14
11
20
50

Output: 64

Input:
8 8
.#......
S..#.B.#
.B..21B#
......#.
...#3B..
#....#..
.4...##.
...#...#
46
17
26
0

Output: 0

Input:
12 12
...#...##...
....#.B.....
...........#
###.....B..#
...#...B....
......S##..#
#..#..#.....
...##.#.#..#
.#...B..B##B
.#..#.....B.
#....#...1..
.....#.....#
26

Output: 0

Input:
12 12
..#.B#......
##...#...#..
.....#.#.1#.
.....#.#B...
B.....S.....
#..........#
.#......B...
...#.##.#...
..#.....#...
....#.......
........B...
..B.##....B.
4

Output: 0

Input:
12 12
............
...........B
..B.........
............
....B.......
............
......B.....
#...........
........B#S.
#..#..#.....
..B......1..
.B..........
40

Output: 30

Input:
12 12
............
.....##.1..#
.......B...#
......#...#.
.B.....S....
....B.......
....B.......
.........#..
B..........#
............
B......B..#.
.....#......
13

Output: 0

Input:
12 12
..B....#..#.
.#..#......B
............
.B..#....#.#
B.....#.....
##....#....B
....#.#...#.
.......#...#
.#.......#.#
#.#..1.##S.#
..#...#.....
B....B....#.
-3

Output: 0

Input:
12 12
............
.B...B......
....#......B
.......#.B..
.#..........
........#.#.
............
............
............
............
S.B...1..B..
......B.....
-8

Output: 0

Input:
12 12
...B...B...#
.S..........
..###.#.##.#
.#.##..B....
.##.1.......
.......#...#
#..#..B#..##
...#.....#..
.....#......
..B.#...#B..
#B#..#.#....
...#.....#.#
38

Output: 20

Input:
12 12
..#.....B...
.#..........
..#.........
..#.B.......
B..#S#...#.#
............
..B##.#.1#..
...#B#...#..
.....B..#...
.....##.....
..B.....#.#.
..##.#...#.#
35

Output: 13

Input:
12 12
...........#
.#B.......#B
.....#......
....#.......
....#.....B.
......#.....
.1.SB#......
..#..#......
...........B
...BB.......
.#.....#....
....#.......
31

Output: 19

Input:
10 10
..........
...5##....
.##..#....
.....2....
...S.B....
..........
.3..B1.#..
.......#..
...B...4..
...#.....#
100
10
14
11
20

Output: 2

Input:
12 12
......B.....
....B...S#..
#....#......
........#...
............
....#...B.B.
.#.....#..#.
..##........
...#.......B
.##.1....#..
#...B......#
..##...B....
42

Output: 0

Input:
20 20
......##.#....#.....
.................#..
##....#........#...#
.......#....1.......
B#...........##..#..
##....##............
........#..#....#.#.
...##.#.5.#.#.#...#.
..#...#..#..........
................#.#.
#.......#..##.##..#.
.#.....#...#S...##..
....#...............
...#.##..#....#..3..
..#.................
#....#..2...B..#....
........#.###.....##
.#.......##.....4.##
.....#.#B.......#..#
###..#......#.......
2
-9
-21
-29
2

Output: 0

Input:
20 20
....#......#.......#
..##.#......##....##
.#....##....#B.3...#
.2..#........#.#..#.
.......#.#..#......S
..#...#........#....
....#.#....#4..5...#
.#.......#..#...#.#.
.#.#...##.#...##..#.
..B..#...#...##.#...
....#.......#.#....#
.#..#.........#...#.
..#.....#.#.#...#...
..........##.#...#..
.##.......1..#....##
#..#...#...#...#....
..B.#..#.........##.
..#..#.#.#.##....#..
......##.#........#.
.#........#....#....
-21
37
38
-21
-12

Output: 22

Input:
20 20
..#.......#.........
.#..................
..#............#....
..................#.
.......S..#.........
.#B.................
..........#.........
...#.....#.....#....
....2#..............
..5...............1.
...#..#.......#.....
#..#................
....................
...#...........B3...
.............#......
...4..#........B....
#......#..#...##...#
....#.............#.
.............#.....#
.#.......#..........
-17
-12
22
28
-22

Output: 0

Input:
20 20
.##.#.............#.
.........B.....#....
....................
..#.#...........##.#
....#.........#...S.
....................
.#..........#.....#.
.....#..#......#...#
...........#....4...
....#......#........
.....#..B#.........#
.#....#....3#......#
#.......#...........
......#......#.....#
.......#.#..##......
..#...5#....#...#...
..........#....#.#..
.....2..............
#........1....#.....
..............B.....
39
-1
49
46
-3

Output: 84

Input:
20 20
....B......#........
......#..#..........
.....#B.#......#.#..
....................
#..#.#....#.#..#....
#...................
..#.........#.......
..#.#...............
...1..B.............
...#...........#..3#
......#.............
.....4...#....#.....
...................#
.....#..............
......#.......5...#.
........#.#.#.#.#...
...#.S..............
....................
.........#....2.....
.........#...#......
13
-14
24
23
18

Output: 5

Input:
20 20
.............#......
.#..........#......#
.B.#.S..45........##
......#.......#.#...
.....#...#..#.......
.....##.............
..B.###.......#.....
....#....#....#.....
.....#..............
#.......#...........
......#.......#.....
....#..#............
..#.....#.#.#.......
.#.....##.##..#.....
..##........3.......
#.....2#..1#........
#.#...#..#.........#
................#..#
...............#..B.
##...#..............
-13
45
14
47
-27

Output: 19

Input:
20 20
....#.#..#........##
.##.....3..#..#.....
.......#.#.....2.#..
.#...#.#......##...#
##..##....#4#.###5#.
#....#..#.#.#...##..
..#....#..#.#...#..#
.S..#......1......#.
.##...#...#........B
...#.##....#.#.....#
.....#..#.###.......
.#....#.#....#.#....
.B#...#.####.......#
.#......####........
...####.###....#....
###.#.............##
.##..##...#..#......
..#.#####....#....B.
...#...........#.##.
..##......#...##.#..
43
33
44
2
-1

Output: 0

Input:
20 20
.#.##....#....##....
#.###....#....##..#.
..............#...##
#..........#.#.1..##
..#.....##..#.##..#.
#.#...#.#......##.#.
...##..#.#...#......
#...##...#..#..#..#.
#S#...#..#.#...#.#3#
.....#...#...#......
..........#..4#..B2.
#...##..#.#.##.....#
.....#...#.##.#...#.
.###....#.#.........
.#.#..###..##.....#.
...##.#...#..##....#
.5.....##B...#.#.##.
...#....#.#...#.###.
#.#......#..#..B....
###.#.#.....##...#..
44
12
23
7
33

Output: 9

Input:
20 20
.............#..#..#
.5..................
.#........#.#B...#..
#.###..B........#.##
.............###.1..
..#...2.....#....#..
....................
.........#4..#......
#......#....#..#..#.
.#.....#.........#.#
.........#.#.#......
.........#..##......
#........#......#.3.
.#............#.###.
.........#.#.#......
##.#....###...#.....
....##......#B......
...#.....##..#......
.#......##.S......#.
...#.....#....#...##
-23
26
-10
-11
30

Output: 0

Input:
10 10
..........
...###....
.##..#....
.......2..
...S.B.1..
.....B....
....B..#..
.......#..
...B......
...#.....#
30
-5

Output: 7

Input:
20 20
....#..#.....#......
..............###...
.#...........#......
..#..#..#...........
......1.......##....
............2....#..
....#......##.......
.....#..#.....B.....
B.......#.#...#.....
..#.....#......#....
.#.4.......#........
.....#..#...#.......
#..#...........#....
....#...............
.....#...........#..
....#.....#.#..5....
#..#................
#............3......
........##...#.#....
...............BS.##
14
-26
-8
-10
-1

Output: 0

Input:
20 20
.#..................
.##....#...3.....#.B
#.###...#..#...B....
.....#..#.#.....#...
#.....#.#..##..#.1#.
.....#.........#....
....#.#......#.....#
..........#.........
...#..#............#
.............#.2..#.
.#.............#....
####.....#.#.#.#...#
...#..#........#...#
#....#.#..#..###....
....#.......B.#..#.B
...........#....4..#
.#..................
..#.#....#...#......
..#...#...##..S.....
.....#.....#.....#..
2
-21
-23
17

Output: 0

Input:
20 20
................#.B.
....................
..#..........B......
....................
....#...............
....................
....................
....................
....................
....................
........S.........4.
..2.................
............1.......
....................
....................
....................
....................
....B........B......
................3...
....................
-22
-22
17
-14

Output: 0

Input:
20 20
.....#..#....#....#.
.#.....#........###.
............B#....#.
..#........#.#.#....
#......#..#.........
.#.......#.........#
#.....##.#.#..#.....
#....#.#...2#..S...B
.........#....#.....
.......#....#..B#...
#....##..#.....#..#.
.........#...#...#..
.......##....#...3..
.#...........#.#..B.
.....14.#.....#..###
....#.....#.#...##..
........##.......#..
#..#...........#....
.....#...#..#.##....
................##..
-3
33
7
18

Output: 17

Input:
20 20
...#....#........#..
.#B##.#...#......#..
..#...##..#.#..##.#.
#...#.#...#..B..#.#.
.....#.#.##.#.#.....
.#.#.##....###.....#
...##...#...#.#...#.
#......##....##.....
#.#..#...#1.#.#B#..#
..#........#..##....
....#...#...##B.#.#.
#..#.#..##.#..3.####
....##.......#..##S.
.........#.....4#.#.
.2.........#......#.
.#...#.#.....#..##.#
..#....#...#.....##.
#..##....#..#..#..#.
#.#...##..#.##.#..#.
..................#.
37
-30
-17
10

Output: 0

Input:
20 20
##.#......##.###..#.
#..#................
#.............#2....
#.............##..#.
...##.....##........
#....#..............
......###.B...##.##.
....#.##........#.#.
...##..#.#..B..##.#.
...#........#.##.#..
......#........#....
....#..#.#......##.#
...#........#.#..#..
.....##.#..#..#.###.
#..S....###.#...#...
....##..#.#..3..#.#.
.........1.......#..
......#...B......#.#
.....4..##.#.....##.
.B#...#.............
38
-3
37
-27

Output: 0

Input:
20 20
#..#...B#..........#
.#.##..#...#..##...B
#....#...........#..
..#..........#..#..#
....#1.....#....#...
...#.#####.#.#...##.
#........#..#..#.#..
.........#.##..#.B.#
............S...#...
.....##...#.....#...
.#.#..##..#......#..
..4.###........#.#..
.....3..#..#......B.
.#.......#....#.....
...##..#...#.#....#.
#......#............
.......2...##.......
.............##.....
....#...............
#.#....#.#..#......#
-5
-26
39
42

Output: 47

Input:
20 20
....#.#.........#...
..##.#.......#.#....
..#......##.#.#...#.
.S##..#.....##...#..
.#...#.......#B#....
#...#......#...##...
...#..........B.#.#.
...........3.#...##.
............#...B#..
..#..#...#.....1....
.....#......#.......
...#...#...##..#.#..
##........#.........
.#....#......#...#..
#.#..#..#...#....2..
.##..........#...#..
..#...........4.....
....#.##.....#..#.B.
#...#..###.#.#....##
............#.......
47
-28
8
-24

Output: 0

Input:
20 20
B.......##..#.#.....
.....#.#....2....#.#
#...##...##..##...#.
.#........1......##.
.......#.##.......##
.#B.#...#..#...#....
#..#.#.......##.#..#
...#..#.###........#
..S....#.#.#.#......
.....#..##....#.#.#.
#B#..#..............
.#3..4#.#.##........
#####.#.............
......###........###
.#........#....#...#
.........#..#..#....
##..#......###.#...#
..#.#..###.....#####
#.#....#.......#...#
..#...#B#.###.#...#.
35
-17
0
-2

Output: 0

Input:
20 20
......##.##....#....
.....#........#..##.
#..#..#.##....###...
.#.......#.........#
#.#.#..##.......##..
#..##......##......#
.....#........##.#4.
#...##.#......#.##.#
#..B..#....##.#..3..
..##..#....#.....#.#
.#..#..##........#..
..#...#.2.#####.....
...#...#..#.........
.###....#.#....#....
#..#.#..B#...#B.....
..#.#.#..##.......#.
.......#.#B#.#......
..S..#.#............
#.......#...#.....1#
.#....#.#..#...#.#.#
22
26
11
33

Output: 0

Input:
10 11
...........
...........
.#..B......
....B.1....
...S.#.....
....B......
....B..#...
.......#...
....B......
...#.....#.
30

Output: 0

Input:
20 20
......#.............
.##.................
...........#.#.##..#
.#..................
.#.#................
............#......#
....................
.B.#................
.B.....#1..B.#.#..2.
....................
..#S................
..........3....#....
......4......#....##
...#..#.#.....#.#...
..........#.........
#..B............#...
#...................
...#.......#........
#..............##...
.................#..
27
22
20
27

Output: 46

Input:
20 20
.##..#.#.#...#..#...
.S.#.....#...#...##.
.#......#...#......#
........#...........
.............#.#..#.
.#..2.#1...#...#..#.
......#.6....#7.#..#
.#....#....#...#...#
..........#.......#.
......#....#..#.....
.#.........#.3#.##8.
.#.###..........#..#
##...##.........###.
..#...#...#.#.....#.
....#.#.4.#....#....
.5##......#.....#..#
..#........##....#.#
#.#.#.#...#......#..
......#.............
..#.....#......#..#.
75
39
54
77
19
20
9
69

Output: 219

Input:
20 20
##.###.#.#..#.#..#..
#..........#.#..#...
..#.#.#.....#.#####.
.###..........#.#.#.
....#...#.........#.
.#.#....#.##.7...##.
..#..S....#....#.##.
..#...5#.#...#.#.###
.....###.#...##.#...
.##..####.#..###.#..
#....#..#...1.#..#..
##..#..............#
..........#..#..#.#.
#....3....#.#....#..
..#.6.#..#.#..##..##
...#.......#...#..#.
.....###.8#...#..#..
....##.4#....#......
#2.##.#.#...#..#####
........#...##...#..
16
-31
44
44
-10
76
-48
30

Output: 84

Input:
20 20
..............#.....
............#.#.....
#......#.#.....#....
...#..........6#....
....................
...#...#..#........#
.............#.#.#..
...#7.........#...#.
..........#...#.....
........2...........
#....#..##.#....##.#
#.#.....#.#....#....
#..........#..#.#...
.#....1..........#..
..#.................
#.....###.#.....#.#.
#....3.....#........
...#4..#.#.S.#..5...
.....#.......8..#.#.
.........#.......#..
9
97
1
3
7
-42
-10
59

Output: 125

Input:
20 20
....................
#...................
..#.................
........2...........
.8..................
.....4......#..S....
....................
....................
...6.........5......
....................
.........#..........
.................1..
....................
....................
...3.......#........
....................
....................
..................7.
....................
....#...............
16
-45
7
49
54
45
-18
-41

Output: 117

Input:
20 20
#................#..
................#.#.
.....##...#.3.7#.#..
......S..#.#........
....4##.#.....1..#..
#..#...#.#..........
..##........##.#....
.2#...##....##....##
##.5..#..#.#..#.#..#
..#.##...#......#...
.......#...#......##
..............##.#.#
.6..##.#........#.#.
.#..#.#.#........#..
......#.#..........#
.#.#....#........#..
..#...........8.##..
...........#...#..#.
.#..........#...#...
#..#..#.#....###....
-28
86
19
56
-43
11
21
-21

Output: 0

Input:
20 20
#................#..
................#.#.
.....##...#.3.7#.#..
......S..#.#........
....4##.#.....1..#..
#..#...#.#..........
..##........##.#....
.2#...##....##....##
##.5..#..#.#..#.#..#
..#.##...#......#...
.......#...#......##
..............##.#.#
.6..##.#........#.#.
.#..#.#.#........#..
......#.#..........#
.#.#....#........#..
..#...........8.##..
...........#...#..#.
.#..........#...#...
#..#..#.#....###....
-28
86
19
56
-43
11
21
-21

Output: 0

Input:
20 20
........#.....##..##
#....##......#..#...
.......#...#.....#..
#...##..#.#.#.#.....
.........#...5..##..
...##...#...#.......
..#.#.....##..#.#.#.
..##.......##....#..
#.....#.....#..###.#
..3S.###........#...
.#..#....#4......#..
.......#.....#.#.#.#
#.#....#..#..#.1..#.
.#...6........#...#.
..#.....#...#.#2..#.
...#.#..#......####.
...........#...#....
##.8.##..###..#....#
...7..#.###....#...#
..#....#..#.........
14
-24
44
65
5
-25
40
99

Output: 75

Input:
20 20
........#......#....
..6..#..............
.2..#1.........S#...
....................
..............#...#.
..............#.....
...#......#......#..
................3.#.
...................#
....#..........#....
..#......#..........
#.............#...#.
.............8......
.............#...#..
....................
..4.....#...#..#....
.#..............#...
...............#..7.
.......#..5..##.....
.........#.....#....
-19
-32
13
7
52
60
-9
77

Output: 45

Input:
20 20
.....#.........#....
..#..##...##........
..........#.#.#.#...
.#.............#....
..............#.....
S#......8..#..#.....
..........###....#..
....#....#...##...##
....#......#.#......
#..#.6..#...........
...#.........3...#..
.......#........#...
...............#....
.....#..21.....#...#
#...........#....#.#
........4.....#...7.
..#..........#......
#................#5.
.##.......#.........
...#...#..........#.
79
-16
28
-16
92
61
60
-38

Output: 180

Input:
20 20
...#.......#..#.#.#.
......#.#.#.##.#....
.....#..#...........
#..##.....#.#.......
....#.............#.
...#.....##...##...#
...#..............#.
.#..#..........##2.#
.....#....##........
..............#....#
#....#.##....#..#...
......7#...##.#.....
#....#6......#...#..
#.##.##....#....#5..
.....#.#..#..###....
..#.S.3..#..#....1##
..#..##..#.......#..
.#......4.##.....#..
#.##.#.#..#.8.......
..#.##....#...#..#.#
44
-43
10
94
30
40
-45
-13

Output: 0

Input:
10 11
...........
...........
.#.B.......
......1....
....SB.....
....B......
...B...#...
.......#...
....B......
...#.....#.
30

Output: 0

Input:
20 20
.###.......#...##...
...S....#.....##.#.#
#....#.....#........
...#...##.#......#..
.....##.#.##...#....
...........#2.......
........#......##...
.......7....##..#.##
..#...#.......#..#..
..#.#....4#..#...5#.
..#....#3#.......##.
...........##..#...#
.......#...#........
............#.#.....
...#........8....#..
............#.#...#.
.#......#6......#...
.##.........#..#..1.
.....###...#.....#..
#.#......##.....#.#.
49
43
83
0
-26
5
36
53

Output: 162

Input:
20 20
.###.......#...##...
...S....#.....##.#.#
#....#.....#........
...#...##.#......#..
.....##.#.##...#....
...........#2.......
........#......##...
.......7....##..#.##
..#...#.......#..#..
..#.#....4#..#...5#.
..#....#3#.......##.
...........##..#...#
.......#...#........
............#.#.....
...#........8....#..
............#.#...#.
.#......#6......#...
.##.........#..#..1.
.....###...#.....#..
#.#......##.....#.#.
49
43
83
0
-26
5
36
53

Output: 162

Input:
5 5
....3
.....
1....
.524.
S....
36
22
12
-23
-19

Output: 0

Input:
10 10
....#.....
....#.#8..
6#.#..##.#
.#.7..4#.S
.#......#.
..#..2....
1......#.#
..3.#.....
......5...
.#.##...#.
8
12
-10
17
49
-7
33
32

Output: 82

Input:
5 5
....2
.51.S
.3..4
.....
.....
-2
44
-14
-22
45

Output: 15

Input:
5 5
3..5.
..1..
.2...
S....
.4...
13
52
31
-37
-1

Output: 0

Input:
7 7
.......
.1###2.
.#...#.
.#.B.#.
.3...4.
..##...
......S
100
100
100
100

Output: 364

Input:
5 5
.....
..4..
3..1.
.5...
2S...
-8
29
31
6
35

Output: 0

Input:
5 5
32...
.5.S1
.....
..4..
.....
11
57
19
-17
-13

Output: 0

Input:
5 5
.....
..S..
5.2..
3.4..
.1...
-4
7
-11
29
-37

Output: 0

Input:
5 5
..2.1
....S
...4.
.....
..35.
-16
31
-40
27
-25

Output: 19

Input:
5 5
.....
.3..5
..14.
.....
..S2.
-24
-38
-3
48
2

Output: 0

Input:
5 5
.3.2.
.S...
..54.
.....
..1..
14
-28
-25
-10
31

Output: 11

Input:
5 5
.B...
...S.
.#...
..1..
#.#..
2

Output: 0

Input:
10 10
...#...#..
...1...#..
.#..3..#..
..#5.7..#.
.4#...##..
....#.#...
..##.#...6
.S.......#
#........#
2#.#B.....
12
30
57
37
80
14
5

Output: 0

Input:
10 10
...#...S##
#B#.....1.
.......#..
3#.B..#...
.........B
......4..#
......B...
.....2....
..........
........#.
36
40
16
-8

Output: 0

Input:
10 10
..........
..1......#
.....2.4.#
........#.
.......S..
..........
.#...3....
..7....6#.
..........
....8...5.
38
8
74
24
32
50
74
27

Output: 210

Input:
10 10
..###2...#
#....#....
#........#
8.....#.#.
..6.#.#...
.#....7.#.
3###...4..
..#...S#.5
.......1..
.##...####
-1
72
63
2
40
49
13
42

Output: 0

Input:
10 10
.##2#...#.
##.6..#..1
#..S...74.
##.......#
#..5#8.#..
...##.3###
.........#
.##.###.#.
#..#.#.##.
#......###
-4
64
10
-4
42
65
6
0

Output: 0

Input:
20 10
...#....5.
..2##.....
.......##.
.......#..
...S##..#.
.#....7...
....#.#...
..#.##.#..
..###.#...
....6#....
##4#31...#
..#......#
#..##..##.
.#.###..#.
..###....#
....#.#...
##.8...#..
.###.#.#.#
.........#
...#..#...
-21
40
47
130
55
155
0
32

Output: 0

Input:
20 10
..........
........S.
........#.
......B...
..3.......
.....B1...
....#.....
..........
..........
..........
..........
.B.....B..
.....2....
....#.....
..........
..........
.....#....
..#.......
..........
........B.
-20
-2
143

Output: 121

Input:
10 10
.#........
..#...#...
#....7..S.
..8...#...
.4..#.....
.#3.......
.2..#.....
.##.....5.
..1##.#6..
......#..#
58
30
47
73
-18
-16
25
63

Output: 262

Input:
20 20
..#..##..##......#..
...#..#..#..##......
....S...#...#....#..
#.#.....###...#...##
##.....##...#..#####
.#..#...........6#..
....#...8.#..#......
.#..5#.#......#.#...
#...........3.#.#...
.##.....##.........#
...##.#....#.##.#...
...............####.
.#.........#...#...2
#.#.#..#.#.#.#......
#.#.1...............
.#....4#......##..#.
.#....#.......#...#.
#........##......#.#
#..###..7#....#..#.#
#...#......#......#.
-22
-65
-29
71
128
-75
180
154

Output: 483

Input:
20 20
.#.######..##.#.....
.......#....##...##.
.#......#7.....#....
##..#..#6........#..
..#.##.##..#..##...#
##............##....
.....#..#........#.#
.#............#..#..
.#.#..##..#.#...#2..
..................#.
.....#1.###......#..
....#..#...#.#....##
..#.#...##...##..4#.
..##.#####.##.#.###.
#...##.3....#.#..##.
..##.......##..#..##
........#..#8...###.
........#..#.#5.....
#.####.....#.#.#...#
.S...#...###.#.#..#.
98
96
52
-35
-79
47
80
-49

Output: 108

Input:
20 20
.........S.5....4...
....................
....................
....................
....................
.........6..........
....................
....................
...............2....
....................
....................
..........7.........
....................
....................
....................
........3...........
....................
.........1..........
.....8..............
....................
-58
165
57
14
91
153
-30
122

Output: 435

Input:
20 20
.........#..........
4...#...............
.#8.......#.....S...
........#.....#.....
.#.##....#....3...#.
...........#........
..............#..#..
.....6.#........#...
.............#.....#
#........#.....#..#.
....###.#......#....
..#.................
#.#........#.###.#..
....#.............1.
.###............#...
...............#...#
.........#.#.....5..
.......#.#..7...2.#.
..........#...#.....
.....#..............
128
91
116
-39
-29
-39
85
171

Output: 335

Input:
20 20
......#..#........#.
##..................
..#..#.....S......##
..##.#.....B..#.#...
#....#.......##...#.
.........#.....##...
.##.#.#......###....
###.#..##......#....
................#...
.#...#...........#.#
.#.#.....#........#.
..#..###..#..#......
.....#..#####..#....
.....###.#.....B...B
.....2...#...####.#.
...1.##.#.4.##...#..
##........#.#...##.#
....##...#.#.#..#...
##....B.#.###....#..
...#3.#..#.##.....#.
-36
81
169
16

Output: 0

Input:
20 20
..#..##..##......#..
...#..#..#..##......
....S...#...#....#..
#.#.....###...#...##
##.....##...#..#####
.#..#...........6#..
....#...8.#..#......
.#..5#.#......#.#...
#...........3.#.#...
.##.....##.........#
...##.#....#.##.#...
...............####.
.#.........#...#...2
#.#.#..#.#.#.#......
#.#.1...............
.#....4#......##..#.
.#....#.......#...#.
#........##......#.#
#..###..7#....#..#.#
#...#......#......#.
-22
-65
-29
71
128
-75
180
154

Output: 483

Input:
10 11
...........
.5.........
.7.....#...
..6..4#....
#.....1....
....3......
..2....#...
#.#......#.
.....S.....
...........
12
36
-31
53
-30
-30
-20

Output: 69

Input:
20 20
....................
....................
....................
....................
....................
....................
....3####...........
....#....#..........
....#.B..#..........
....#....#..........
.....###......####2.
..............#...#.
.....1####....#.B.#.
.....#...#....#.....
.....#.B.#.....#....
.....#..............
......###...........
....................
....................
...................S
100
101
102

Output: 201

Input:
20 20
....................
....................
....................
...........4###.....
...........#...#....
...........#.B.#....
....3####..#...#....
....#....#.#..#.....
....#.B..#.#.#......
....#....#..........
.....###......####2.
..............#...#.
.....1####....#.B.#.
.....#...#....#.....
.....#.B.#.....#....
.....#..............
......###...........
....................
....................
...................S
100
101
102
103

Output: 274


The only line contains two integers $$$n$$$ and $$$x$$$ ($$$2 \le n \le 500; 1 \le x \le 500$$$).

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lr(a) memset(a,0,sizeof(a))
const int N=550,mod=998244353;
int n,x,C[N][N],pw[N][N]; //组合数&幂
int dp[N][N],ans; //dp[n][x]
void init(){
	lr(dp),lr(pw),lr(C);
	for(int i=0;i<=510;i++)C[i][0]=pw[i][0]=1;         //还有这里！i要从0开始，不然C[n][n]就等于0鸟QWQWQWQ
	for(int i=1;i<=510;i++)
	for(int j=1;j<=510;j++)
		C[i][j]=(C[i-1][j-1]+C[i-1][j]+mod)%mod,
		pw[i][j]=(pw[i][j-1]*i+mod)%mod;
	scanf("%lld%lld",&n,&x);
}
int solve(){
	for(int peo=2;peo<=n;peo++){
		for(int i=1;i<=x;i++){
			if(i<=peo-1)dp[peo][i]=(pw[i][peo]-pw[i-1][peo]+mod)%mod;   //错在这里了！ i和peo刚开始写反了!qwq
			else{
				for(int k=2;k<=peo;k++){
					dp[peo][i]=(dp[peo][i]+dp[k][i-(peo-1)]%mod*C[peo][k]%mod*pw[peo-1][peo-k]+mod)%mod;
				}
			}
		}
	}
	for(int maxn=1;maxn<=x;maxn++){
		ans=(ans+dp[n][maxn]+mod)%mod;
	}
	return ans;
}
signed main(){
	init();
	printf("%lld\n",solve());
}

Print one integer — the number of ways to choose the initial health points for each hero $$$a_i$$$, where $$$1 \le a_i \le x$$$, so that there is no winner of the fight, taken modulo $$$998244353$$$.

There are $$$n$$$ heroes fighting in the arena. Initially, the $$$i$$$-th hero has $$$a_i$$$ health points.The fight in the arena takes place in several rounds. At the beginning of each round, each alive hero deals $$$1$$$ damage to all other heroes. Hits of all heroes occur simultaneously. Heroes whose health is less than $$$1$$$ at the end of the round are considered killed.If exactly $$$1$$$ hero remains alive after a certain round, then he is declared the winner. Otherwise, there is no winner.Your task is to calculate the number of ways to choose the initial health points for each hero $$$a_i$$$, where $$$1 \le a_i \le x$$$, so that there is no winner of the fight. The number of ways can be very large, so print it modulo $$$998244353$$$. Two ways are considered different if at least one hero has a different amount of health. For example, $$$[1, 2, 1]$$$ and $$$[2, 1, 1]$$$ are different.

Input: 2 5

Output: 5

Output: 5

Output: 5

Output: 5

Output: 5

Output: 5

Input: 3 3

Output: 15

Output: 15

Output: 15

Output: 15

Output: 15

Output: 15

Input: 5 4

Output: 1024

Output: 1024

Output: 1024

Output: 1024

Output: 1024

Output: 1024

Input: 13 37

Output: 976890680

Output: 976890680

Output: 976890680

Output: 976890680

Output: 976890680

Output: 976890680

Input: 5 40

Output: 6613840

Output: 6613840

Output: 6613840

Output: 6613840

Output: 6613840

Output: 6613840

Input: 4 33

Output: 74061

Output: 74061

Output: 74061

Output: 74061

Output: 74061

Output: 74061

Input: 6 26

Output: 37929526

Output: 37929526

Output: 37929526

Output: 37929526

Output: 37929526

Output: 37929526

Input: 7 22

Output: 433133716

Output: 433133716

Output: 433133716

Output: 433133716

Output: 433133716

Output: 433133716

Input: 10 15

Output: 801988713

Output: 801988713

Output: 801988713

Output: 801988713

Output: 801988713

Output: 801988713

Input: 2 500

Output: 500

Output: 500

Output: 500

Output: 500

Output: 500

Output: 500

Input: 3 500

Output: 375500

Output: 375500

Output: 375500

Output: 375500

Output: 375500

Output: 375500

Input: 4 500

Output: 250499992

Output: 250499992

Output: 250499992

Output: 250499992

Output: 250499992

Output: 250499992

Input: 5 500

Output: 940552292

Output: 940552292

Output: 940552292

Output: 940552292

Output: 940552292

Output: 940552292

Input: 8 333

Output: 97191222

Output: 97191222

Output: 97191222

Output: 97191222

Output: 97191222

Output: 97191222

Input: 6 478

Output: 28573939

Output: 28573939

Output: 28573939

Output: 28573939

Output: 28573939

Output: 28573939

Input: 11 345

Output: 932713620

Output: 932713620

Output: 932713620

Output: 932713620

Output: 932713620

Output: 932713620

Input: 15 255

Output: 259067064

Output: 259067064

Output: 259067064

Output: 259067064

Output: 259067064

Output: 259067064

Input: 13 337

Output: 434551606

Output: 434551606

Output: 434551606

Output: 434551606

Output: 434551606

Output: 434551606

Input: 10 500

Output: 263020220

Output: 263020220

Output: 263020220

Output: 263020220

Output: 263020220

Output: 263020220

Input: 25 500

Output: 571274201

Output: 571274201

Output: 571274201

Output: 571274201

Output: 571274201

Output: 571274201

Input: 49 499

Output: 816854007

Output: 816854007

Output: 816854007

Output: 816854007

Output: 816854007

Output: 816854007

Input: 50 500

Output: 165073862

Output: 165073862

Output: 165073862

Output: 165073862

Output: 165073862

Output: 165073862

Input: 99 499

Output: 796227309

Output: 796227309

Output: 796227309

Output: 796227309

Output: 796227309

Output: 796227309

Input: 99 500

Output: 424278934

Output: 424278934

Output: 424278934

Output: 424278934

Output: 424278934

Output: 424278934

Input: 111 500

Output: 802132036

Output: 802132036

Output: 802132036

Output: 802132036

Output: 802132036

Output: 802132036

Input: 154 500

Output: 924911664

Output: 924911664

Output: 924911664

Output: 924911664

Output: 924911664

Output: 924911664

Input: 200 500

Output: 458968932

Output: 458968932

Output: 458968932

Output: 458968932

Output: 458968932

Output: 458968932

Input: 222 500

Output: 382157018

Output: 382157018

Output: 382157018

Output: 382157018

Output: 382157018

Output: 382157018

Input: 285 499

Output: 987275082

Output: 987275082

Output: 987275082

Output: 987275082

Output: 987275082

Output: 987275082

Input: 300 500

Output: 567125736

Output: 567125736

Output: 567125736

Output: 567125736

Output: 567125736

Output: 567125736

Input: 365 500

Output: 552203508

Output: 552203508

Output: 552203508

Output: 552203508

Output: 552203508

Output: 552203508

Input: 444 500

Output: 563065086

Output: 563065086

Output: 563065086

Output: 563065086

Output: 563065086

Output: 563065086

Input: 444 499

Output: 835857576

Output: 835857576

Output: 835857576

Output: 835857576

Output: 835857576

Output: 835857576

Input: 500 333

Output: 736893443

Output: 736893443

Output: 736893443

Output: 736893443

Output: 736893443

Output: 736893443

Input: 500 500

Output: 587613361

Output: 587613361

Output: 587613361

Output: 587613361

Output: 587613361

Output: 587613361

Input: 499 499

Output: 772771385

Output: 772771385

Output: 772771385

Output: 772771385

Output: 772771385

Output: 772771385

Input: 111 222

Output: 460833105

Output: 460833105

Output: 460833105

Output: 460833105

Output: 460833105

Output: 460833105

Input: 123 433

Output: 632273638

Output: 632273638

Output: 632273638

Output: 632273638

Output: 632273638

Output: 632273638

Input: 383 477

Output: 158983764

Output: 158983764

Output: 158983764

Output: 158983764

Output: 158983764

Output: 158983764

Input: 99 333

Output: 897436821

Output: 897436821

Output: 897436821

Output: 897436821

Output: 897436821

Output: 897436821

Input: 484 497

Output: 320480021

Output: 320480021

Output: 320480021

Output: 320480021

Output: 320480021

Output: 320480021

Input: 411 77

Output: 525290835

Output: 525290835

Output: 525290835

Output: 525290835

Output: 525290835

Output: 525290835

Input: 124 212

Output: 806210307

Output: 806210307

Output: 806210307

Output: 806210307

Output: 806210307

Output: 806210307

Input: 369 404

Output: 345642117

Output: 345642117

Output: 345642117

Output: 345642117

Output: 345642117

Output: 345642117

Input: 246 389

Output: 778435960

Output: 778435960

Output: 778435960

Output: 778435960

Output: 778435960

Output: 778435960

Input: 92 270

Output: 125864547

Output: 125864547

Output: 125864547

Output: 125864547

Output: 125864547

Output: 125864547

Input: 271 208

Output: 80367024

Output: 80367024

Output: 80367024

Output: 80367024

Output: 80367024

Output: 80367024

Input: 289 466

Output: 807999264

Output: 807999264

Output: 807999264

Output: 807999264

Output: 807999264

Output: 807999264

Input: 249 320

Output: 405917309

Output: 405917309

Output: 405917309

Output: 405917309

Output: 405917309

Output: 405917309

Input: 91 367

Output: 369540872

Output: 369540872

Output: 369540872

Output: 369540872

Output: 369540872

Output: 369540872

Input: 39 78

Output: 146956559

Output: 146956559

Output: 146956559

Output: 146956559

Output: 146956559

Output: 146956559

Input: 465 367

Output: 135201268

Output: 135201268

Output: 135201268

Output: 135201268

Output: 135201268

Output: 135201268

Input: 34 177

Output: 771060153

Output: 771060153

Output: 771060153

Output: 771060153

Output: 771060153

Output: 771060153

Input: 460 235

Output: 27900542

Output: 27900542

Output: 27900542

Output: 27900542

Output: 27900542

Output: 27900542

Input: 421 44

Output: 312830719

Output: 312830719

Output: 312830719

Output: 312830719

Output: 312830719

Output: 312830719

Input: 390 208

Output: 709071139

Output: 709071139

Output: 709071139

Output: 709071139

Output: 709071139

Output: 709071139

Input: 251 203

Output: 921135826

Output: 921135826

Output: 921135826

Output: 921135826

Output: 921135826

Output: 921135826

Input: 191 22

Output: 285057520

Output: 285057520

Output: 285057520

Output: 285057520

Output: 285057520

Output: 285057520

Input: 198 236

Output: 93097976

Output: 93097976

Output: 93097976

Output: 93097976

Output: 93097976

Output: 93097976

Input: 376 314

Output: 795015160

Output: 795015160

Output: 795015160

Output: 795015160

Output: 795015160

Output: 795015160

Input: 26 178

Output: 373528200

Output: 373528200

Output: 373528200

Output: 373528200

Output: 373528200

Output: 373528200

Input: 189 92

Output: 283119998

Output: 283119998

Output: 283119998

Output: 283119998

Output: 283119998

Output: 283119998

Input: 13 93

Output: 962803010

Output: 962803010

Output: 962803010

Output: 962803010

Output: 962803010

Output: 962803010

Input: 71 439

Output: 299896905

Output: 299896905

Output: 299896905

Output: 299896905

Output: 299896905

Output: 299896905

Input: 438 46

Output: 312807374

Output: 312807374

Output: 312807374

Output: 312807374

Output: 312807374

Output: 312807374

Input: 357 328

Output: 186454845

Output: 186454845

Output: 186454845

Output: 186454845

Output: 186454845

Output: 186454845

Input: 384 467

Output: 946997121

Output: 946997121

Output: 946997121

Output: 946997121

Output: 946997121

Output: 946997121

Input: 371 306

Output: 512015273

Output: 512015273

Output: 512015273

Output: 512015273

Output: 512015273

Output: 512015273

Input: 265 181

Output: 178439722

Output: 178439722

Output: 178439722

Output: 178439722

Output: 178439722

Output: 178439722

Input: 311 33

Output: 836810892

Output: 836810892

Output: 836810892

Output: 836810892

Output: 836810892

Output: 836810892

Input: 298 51

Output: 631137022

Output: 631137022

Output: 631137022

Output: 631137022

Output: 631137022

Output: 631137022

Input: 193 402

Output: 490804249

Output: 490804249

Output: 490804249

Output: 490804249

Output: 490804249

Output: 490804249

Input: 119 371

Output: 207908744

Output: 207908744

Output: 207908744

Output: 207908744

Output: 207908744

Output: 207908744

Input: 459 365

Output: 266156666

Output: 266156666

Output: 266156666

Output: 266156666

Output: 266156666

Output: 266156666

Input: 222 422

Output: 858431457

Output: 858431457

Output: 858431457

Output: 858431457

Output: 858431457

Output: 858431457

Input: 195 15

Output: 518355052

Output: 518355052

Output: 518355052

Output: 518355052

Output: 518355052

Output: 518355052

Input: 224 375

Output: 555865043

Output: 555865043

Output: 555865043

Output: 555865043

Output: 555865043

Output: 555865043

Input: 246 348

Output: 875068738

Output: 875068738

Output: 875068738

Output: 875068738

Output: 875068738

Output: 875068738

Input: 291 150

Output: 491847623

Output: 491847623

Output: 491847623

Output: 491847623

Output: 491847623

Output: 491847623

Input: 171 489

Output: 316053655

Output: 316053655

Output: 316053655

Output: 316053655

Output: 316053655

Output: 316053655

Input: 162 427

Output: 10603436

Output: 10603436

Output: 10603436

Output: 10603436

Output: 10603436

Output: 10603436

Input: 350 463

Output: 580010430

Output: 580010430

Output: 580010430

Output: 580010430

Output: 580010430

Output: 580010430

Input: 161 37

Output: 141211019

Output: 141211019

Output: 141211019

Output: 141211019

Output: 141211019

Output: 141211019

Input: 500 1

Output: 1

Output: 1

Output: 1

Output: 1

Output: 1

Output: 1

Input: 164 500

Output: 411608690

Output: 411608690

Output: 411608690

Output: 411608690

Output: 411608690

Output: 411608690

Input: 499 500

Output: 724043052

Output: 724043052

Output: 724043052

Output: 724043052

Output: 724043052

Output: 724043052


The first and the only line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^5, 1 \le k \le \min(2^n - 1, 10^9)$$$) — the number of rounds in the tournament and the number of outcomes that sponsors can change.

#include <cstdio>
#include <algorithm>
using LL = long long;
const int N = 1e5 + 100, P = 1e9 + 7;
int n, num;
int fac[N], ifac[N];
int ans = 0;
int qpow(int x, int y = P - 2)
{
    int res = 1;
    for (; y; y >>= 1, x = LL(x) * x % P) if (y & 1) res = LL(x) * res % P;
    return res;
}
void adj(int &x){ x += (x >> 31) & P; }
int C(int x, int y){ return LL(fac[x]) * ifac[y] % P * ifac[x - y] % P; }
int main()
{
    scanf("%d %d", &n, &num);
    fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
    for (int i = 2; i <= n; ++i) fac[i] = LL(i) * fac[i - 1] % P;
    ifac[n] = qpow(fac[n]);
    for (int i = n - 1; i > 1; --i) ifac[i] = LL(i + 1) * ifac[i + 1] % P;
    num = std::min(n, num);
    for (int i = 0; i <= num; ++i) adj(ans += C(n, i) - P);
    printf("%d\n", ans);
    return 0;
}

NoteIn the first example, there is only one match between players $$$1$$$ and $$$2$$$, so the sponsors can always make player $$$2$$$ wins.The tournament grid from the second example is shown in the picture in the statement.

Print exactly one integer — the minimum number of the winner modulo $$$10^9 + 7$$$

Madoka decided to entrust the organization of a major computer game tournament "OSU"!In this tournament, matches are held according to the "Olympic system". In other words, there are $$$2^n$$$ participants in the tournament, numbered with integers from $$$1$$$ to $$$2^n$$$. There are $$$n$$$ rounds in total in the tournament. In the $$$i$$$-th round there are $$$2^{n - i}$$$ matches between two players (one of whom is right, the other is left), after which the winners go further along the tournament grid, and the losing participants are eliminated from the tournament. Herewith, the relative order in the next round does not change. And the winner of the tournament — is the last remaining participant.But the smaller the participant's number, the more he will pay Madoka if he wins, so Madoka wants the participant with the lowest number to win. To do this, she can arrange the participants in the first round as she likes, and also determine for each match who will win — the participant on the left or right.But Madoka knows that tournament sponsors can change the winner in matches no more than $$$k$$$ times. (That is, if the participant on the left won before the change, then the participant on the right will win after the change, and if the participant on the right won, then the participant on the left will win after the change).  So, the first image shows the tournament grid that Madoka made, where the red lines denote who should win the match. And the second one shows the tournament grid, after one change in the outcome of the match by sponsors (a match between $$$1$$$ and $$$3$$$ players). Print the minimum possible number of the winner in the tournament, which Madoka can get regardless of changes in sponsors. But since the answer can be very large, output it modulo $$$10^9 + 7$$$. Note that we need to minimize the answer, and only then take it modulo.

Input: 1 1

Output: 2

Output: 2

Output: 2

Output: 2

Output: 2

Output: 2

Output: 2

Output: 2

Input: 2 1

Output: 3

Output: 3

Output: 3

Output: 3

Output: 3

Output: 3

Output: 3

Output: 3

Output: 3

Input: 3 2

Output: 7

Output: 7

Output: 7

Output: 7

Output: 7

Output: 7

Output: 7

Output: 7

Output: 7

Input: 5 3

Output: 26

Output: 26

Output: 26

Output: 26

Output: 26

Output: 26

Output: 26

Output: 26

Output: 26

Input: 5 5

Output: 32

Output: 32

Output: 32

Output: 32

Output: 32

Output: 32

Output: 32

Output: 32

Input: 5 31

Output: 32

Output: 32

Output: 32

Output: 32

Output: 32

Output: 32

Output: 32

Output: 32

Input: 11994 11995

Output: 685385528

Output: 685385528

Output: 685385528

Output: 685385528

Output: 685385528

Output: 685385528

Output: 685385528

Output: 685385528

Input: 99999 3123

Output: 575224395

Output: 575224395

Output: 575224395

Output: 575224395

Output: 575224395

Output: 575224395

Output: 575224395

Output: 575224395

Output: 575224395

Input: 100000 1000000000

Output: 607723520

Output: 607723520

Output: 607723520

Output: 607723520

Output: 607723520

Output: 607723520

Output: 607723520

Output: 607723520

Input: 100000 1

Output: 100001

Output: 100001

Output: 100001

Output: 100001

Output: 100001

Output: 100001

Output: 100001

Output: 100001

Output: 100001

Input: 100000 13

Output: 185515077

Output: 185515077

Output: 185515077

Output: 185515077

Output: 185515077

Output: 185515077

Output: 185515077

Output: 185515077

Output: 185515077

Input: 100000 53228

Output: 871774727

Output: 871774727

Output: 871774727

Output: 871774727

Output: 871774727

Output: 871774727

Output: 871774727

Output: 871774727

Output: 871774727

Input: 87532 32150

Output: 165162987

Output: 165162987

Output: 165162987

Output: 165162987

Output: 165162987

Output: 165162987

Output: 165162987

Output: 165162987

Output: 165162987

Input: 30 99999999

Output: 73741817

Output: 73741817

Output: 73741817

Output: 73741817

Output: 73741817

Output: 73741817

Output: 73741817

Output: 73741817


The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018). n is the initial value of variable a, and m is the initial value of variable b.

#include<iostream>
using namespace std;
long long n,m;
int main()
{
    cin>>n>>m;
    while(n&&m)
        if(n>=2*m)n%=2*m;
        else if(m>=2*n)m%=2*n;
        else break;
    cout<<n<<' '<<m<<endl;
    return 0;
}

NoteExplanations to the samples: a = 12, b = 5  a = 2, b = 5  a = 2, b = 1  a = 0, b = 1; a = 31, b = 12  a = 7, b = 12.

Print two integers — the values of a and b after the end of the process.

You have two variables a and b. Consider the following sequence of actions performed with these variables: If a = 0 or b = 0, end the process. Otherwise, go to step 2; If a ≥ 2·b, then set the value of a to a - 2·b, and repeat step 1. Otherwise, go to step 3; If b ≥ 2·a, then set the value of b to b - 2·a, and repeat step 1. Otherwise, end the process.Initially the values of a and b are positive integers, and so the process will be finite.You have to determine the values of a and b after the process ends.

Input: 12 5

Output: 0 1

Input: 31 12

Output: 7 12

Input: 1000000000000000000 7

Output: 8 7

Input: 31960284556200 8515664064180

Output: 14928956427840 8515664064180

Input: 1000000000000000000 1000000000000000000

Output: 1000000000000000000 1000000000000000000

Input: 1 1000

Output: 1 0

Input: 1 1000000

Output: 1 0

Input: 1 1000000000000000

Output: 1 0

Input: 1 99999999999999999

Output: 1 1

Input: 1 4

Output: 1 0

Input: 1000000000000001 500000000000000

Output: 1 0

Input: 1 1000000000000000000

Output: 1 0

Input: 2 4

Output: 2 0

Input: 2 1

Output: 0 1

Input: 6 19

Output: 6 7

Input: 22 5

Output: 0 1

Input: 10000000000000000 100000000000000001

Output: 0 1

Input: 1 1000000000000

Output: 1 0

Input: 2 1000000000000000

Output: 2 0

Input: 2 10

Output: 2 2

Input: 51 100

Output: 51 100

Input: 3 1000000000000000000

Output: 3 4

Input: 1000000000000000000 3

Output: 4 3

Input: 1 10000000000000000

Output: 1 0

Input: 8796203 7556

Output: 1019 1442

Input: 5 22

Output: 1 0

Input: 1000000000000000000 1

Output: 0 1

Input: 1 100000000000

Output: 1 0

Input: 2 1000000000000

Output: 2 0

Input: 5 4567865432345678

Output: 5 8

Input: 576460752303423487 288230376151711743

Output: 1 1

Input: 499999999999999999 1000000000000000000

Output: 3 2

Input: 1 9999999999999

Output: 1 1

Input: 103 1000000000000000000

Output: 103 196

Input: 7 1

Output: 1 1

Input: 100000000000000001 10000000000000000

Output: 1 0

Input: 5 10

Output: 5 0

Input: 7 11

Output: 7 11

Input: 1 123456789123456

Output: 1 0

Input: 5000000000000 100000000000001

Output: 0 1

Input: 1000000000000000 1

Output: 0 1

Input: 1000000000000000000 499999999999999999

Output: 2 3

Input: 10 5

Output: 0 5

Input: 9 18917827189272

Output: 9 0

Input: 179 100000000000497000

Output: 179 270

Input: 5 100000000000001

Output: 1 1

Input: 5 20

Output: 5 0

Input: 100000001 50000000

Output: 1 0

Input: 345869461223138161 835002744095575440

Output: 1 0

Input: 8589934592 4294967296

Output: 0 4294967296

Input: 4 8

Output: 4 0

Input: 1 100000000000000000

Output: 1 0

Input: 1000000000000000000 333333333333333

Output: 1000 1333

Input: 25 12

Output: 1 0

Input: 24 54

Output: 0 6

Input: 6 12

Output: 6 0

Input: 129200000000305 547300000001292

Output: 1 0

Input: 1000000000000000000 49999999999999999

Output: 20 39

Input: 1 2

Output: 1 0

Input: 1 123456789876

Output: 1 0

Input: 2 3

Output: 2 3

Input: 1 3

Output: 1 1

Input: 1 1

Output: 1 1

Input: 19 46

Output: 3 2

Input: 3 6

Output: 3 0

Input: 129 1000000000000000000

Output: 1 0

Input: 12 29

Output: 0 1

Input: 8589934592 2147483648

Output: 0 2147483648

Input: 2147483648 8589934592

Output: 2147483648 0

Input: 5 6

Output: 5 6

Input: 1000000000000000000 2

Output: 0 2

Input: 2 7

Output: 2 3

Input: 17174219820754872 61797504734333370

Output: 17174219820754872 27449065092823626

Input: 49 100

Output: 1 0

Input: 7 17

Output: 1 1

Input: 1000000000000000000 10000001

Output: 0 1

Input: 49999999999999999 2

Output: 3 2

Input: 49999999999999999 1

Output: 1 1

Input: 576460752303423487 2

Output: 3 2

Input: 19395 19395

Output: 19395 19395

Input: 19394 19394

Output: 19394 19394


The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).

/*input
10 3 5 2 3
*/
#include <bits/stdc++.h>
using namespace std;
long long c,wr,wb,hr,hb,ans=0;
long long get(int i){
	return i*hr+hb*int((c-i*wr)/wb);
}
int main(){
	cin>>c>>hr>>hb>>wr>>wb;
	if(hb*wr>hr*wb){
		swap(hr,hb);
		swap(wr,wb);
	}
	if(wr>1e4){
		for(int i=0;i*wr<=c;++i)
			ans=max(ans,get(i));
	}
	else if(wb>1e4){
		for(int i=0;i*wb<=c;++i)
			ans=max(ans,i*hb+hr*int((c-i*wb)/wr));
	}
	else{
		for(int i=0;i<1e7&&i*wb<=c;i++){
			ans=max(ans,i*hb+hr*int((c-i*wb)/wr));
		}
	}
	cout<<ans;
	//assert(0);
}

NoteIn the sample test Om Nom can eat two candies of each type and thus get 16 joy units.

Print a single integer — the maximum number of joy units that Om Nom can get.

A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?  One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.

Input: 10 3 5 2 3

Output: 16

Input: 5 3 1 6 7

Output: 0

Input: 982068341 55 57 106 109

Output: 513558662

Input: 930064129 32726326 25428197 83013449 64501049

Output: 363523396

Input: 927155987 21197 15994 54746 41309

Output: 358983713

Input: 902303498 609628987 152407246 8 2

Output: 68758795931537065

Input: 942733698 9180 9072 1020 1008

Output: 8484603228

Input: 951102310 39876134 24967176 70096104 43888451

Output: 539219654

Input: 910943911 107 105 60 59

Output: 1624516635

Input: 910943911 38162 31949 67084 56162

Output: 518210503

Input: 910943911 9063 9045 1007 1005

Output: 8198495199

Input: 903796108 270891702 270891702 1 1

Output: 244830865957095816

Input: 936111602 154673223 309346447 1 2

Output: 144791399037089047

Input: 947370735 115930744 347792233 1 3

Output: 109829394468167085

Input: 958629867 96557265 386229061 1 4

Output: 92562678344491221

Input: 969889000 84931386 424656931 1 5

Output: 82374017230131800

Input: 925819493 47350513 28377591 83230978 49881078

Output: 520855643

Input: 934395168 119 105 67 59

Output: 1662906651

Input: 934395168 29208 38362 51342 67432

Output: 531576348

Input: 934395168 9171 9045 1019 1005

Output: 8409556512

Input: 946401698 967136832 483568416 2 1

Output: 457649970001570368

Input: 962693577 967217455 967217455 2 2

Output: 465567015261784540

Input: 989976325 646076560 969114840 2 3

Output: 319800249268721000

Input: 901235456 485501645 971003291 2 4

Output: 218775648435471424

Input: 912494588 389153108 972882772 2 5

Output: 177550052841687584

Input: 995503930 29205027 18903616 51333090 33226507

Output: 565303099

Input: 983935533 115 108 65 61

Output: 1742049794

Input: 983935533 33986 27367 59737 48104

Output: 559787479

Input: 983935533 7105 7056 1015 1008

Output: 6887548731

Input: 994040035 740285170 246761723 3 1

Output: 245291032098926983

Input: 905299166 740361314 493574209 3 2

Output: 223416160034288041

Input: 911525551 740437472 740437472 3 3

Output: 224975891301803200

Input: 922784684 566833132 755777509 3 4

Output: 174354977531116762

Input: 955100178 462665160 771108601 3 5

Output: 147297192414486195

Input: 949164751 36679609 23634069 64467968 41539167

Output: 537909080

Input: 928443151 60 63 106 112

Output: 525533853

Input: 928443151 25031 33442 43995 58778

Output: 528241752

Input: 928443151 1006 1012 1006 1012

Output: 928443150

Input: 936645623 540336743 135084185 4 1

Output: 126526011319256470

Input: 947904756 540408420 270204210 4 2

Output: 128063927875111380

Input: 959163888 540480074 405360055 4 3

Output: 129602242291091928

Input: 970423020 540551739 540551739 4 4

Output: 131140962756657945

Input: 976649406 455467553 569334442 4 5

Output: 111208028918928288

Input: 923881933 18531902 53987967 32570076 94884602

Output: 524563246

Input: 977983517 57 63 101 112

Output: 551931291

Input: 977983517 29808 22786 52389 40047

Output: 556454318

Input: 977983517 9009 9108 1001 1012

Output: 8801851608

Input: 984283960 367291526 73458305 5 1

Output: 72303831537144592

Input: 990510345 367358723 146943489 5 2

Output: 72774523091497887

Input: 901769477 367425909 220455545 5 3

Output: 66266693959035917

Input: 907995862 367493085 293994468 5 4

Output: 66736440098722854

Input: 924287742 367560271 367560271 5 5

Output: 67946290439275508

Input: 1000000000 1000 999 100 1000000000

Output: 10000000000

Input: 999999999 10 499999995 2 99999999

Output: 4999999995

Input: 999999999 1 1000000000 2 1000000000

Output: 499999999

Input: 999999997 2 999999997 2 999999997

Output: 999999997

Input: 1000000000 1 1 11 11

Output: 90909090

Input: 999999999 999999998 5 999999999 5

Output: 999999998

Input: 100000001 3 100000000 3 100000001

Output: 100000000

Input: 999999999 2 3 1 2

Output: 1999999998

Input: 1000000000 2 1 3 4

Output: 666666666

Input: 999999999 10000 494999 2 99

Output: 4999999994999

Input: 1000000000 1 1 1 1

Output: 1000000000

Input: 998999 1000 999 1000 999

Output: 998999

Input: 3 100 101 2 3

Output: 101

Input: 345415838 13999 13997 13999 13997

Output: 345415838

Input: 5000005 3 2 5 1

Output: 10000010

Input: 1000000000 1 1 1 1000000000

Output: 1000000000

Input: 999999999 3 2 10 3

Output: 666666666

Input: 1000000000 1000 1000 1 1

Output: 1000000000000

Input: 200000001 100000002 1 100000001 1

Output: 200000002

Input: 100000000 1000000000 1 100000001 1

Output: 100000000

Input: 1000000000 99 100 1 2

Output: 99000000000

Input: 1000000000 5 5 1 1

Output: 5000000000

Input: 1000000000 1 1000000000 1 1000000000

Output: 1000000000


The first line contains a single integer n (0 ≤ n ≤ 1000).

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int N=1010;
const int MOD=(int)1e6+3;
ll n,dp[N],twoPow[2*N];

void init() {
    twoPow[0]=1;
    for(int i=1;i<=2*N;i++)
        twoPow[i]=(twoPow[i-1]*2)%MOD;
    memset(dp,-1,sizeof(dp));
}

ll cookie(ll a) {
    return (twoPow[a-1]*(twoPow[a]+1))%MOD;
}

ll solve(ll a) {
    if (a<=0)
        return 0;
    if (dp[a]!=-1)
        return dp[a];
    dp[a]=cookie(a)%MOD;
    for(int i=1;a-i>0;i++) {
        dp[a]=(dp[a]+solve(a-i)*twoPow[i-1])%MOD;
    }
    return dp[a];
}

int main() {
    init();
    cin >> n;
    cout << (twoPow[2*n]-solve(n)+MOD)%MOD << endl;
}

NoteIf the box possesses the base of 23 × 23 (as in the example), then the cookies will be put there in the following manner:

Print the single number, equal to the number of empty cells in the box. The answer should be printed modulo 106 + 3.

Fangy collects cookies. Once he decided to take a box and put cookies into it in some way. If we take a square k × k in size, divided into blocks 1 × 1 in size and paint there the main diagonal together with cells, which lie above it, then the painted area will be equal to the area occupied by one cookie k in size. Fangy also has a box with a square base 2n × 2n, divided into blocks 1 × 1 in size. In a box the cookies should not overlap, and they should not be turned over or rotated. See cookies of sizes 2 and 4 respectively on the figure:    To stack the cookies the little walrus uses the following algorithm. He takes out of the repository the largest cookie which can fit in some place in the box and puts it there. Everything could be perfect but alas, in the repository the little walrus has infinitely many cookies of size 2 and larger, and there are no cookies of size 1, therefore, empty cells will remain in the box. Fangy wants to know how many empty cells will be left in the end.


The only line contains three integers maxn, maxa and q (1 ≤ maxn ≤ 30 000; maxn ≤ maxa ≤ 109; 104 ≤ q ≤ 105 + 129).

#include <bits/stdc++.h>
using namespace std;

const int p1 = 998244353, p2 = 469762049, Maxn = 1 << 17 | 5;
int maxn, maxa, mod, rev[Maxn];
int tot, l[Maxn];
const long long lcm = p1 * (long long) p2;
void get_rev(int len)
{
	for (int i = 0; i < len; i++)
		rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (len >> 1));
}
long long fast_pow(long long x, long long y, int p)
{
	long long ans = 1, now = x;
	while (y)
	{
		if (y & 1) ans = ans * now % p;
		now = now * now % p;
		y >>= 1;
	}
	return ans;
}
void NTT(int now[], int len, int p, bool type = false)
{
	l[0] = 1;
	int inv3 = fast_pow(3, p - 2, p);
	for (int i = 0; i < len; i++)
		if (i < rev[i]) swap(now[i], now[rev[i]]);
	for (int i = 1; i < len; i <<= 1)
	{
		long long w = fast_pow(type ? inv3 : 3, (p - 1) / (i << 1), p);
		for (int j = 1; j < i; j++)
			l[j] = l[j - 1] * w % p;
		for (int j = 0; j < len; j += (i << 1))
			for (int k = j; k < i + j; k++)
			{
				int x = now[i + k] * (long long) l[k - j] % p, y = now[k];
				now[k] = (x + y) % p;
				now[i + k] = (y - x + p) % p;
			}
	}
	if (type)
	{
		long long inv = fast_pow(len, p - 2, p);
		for (int i = 0; i < len; i++)
			now[i] = now[i] * inv % p;
	}
}
void multi(int x[], int y[], int result[], int len, int p)
{
	NTT(x, len, p), NTT(y, len, p);
	for (int i = 0; i < len; i++)
		result[i] = x[i] * (long long) y[i] % p;
	NTT(result, len, p, true);
}
long long mul(long long x, long long y)
{
	return ((unsigned long long) x * y - (long long) (x / (long double) lcm * y) * lcm + lcm) % lcm;
}
void multi(int x[], int y[], int result[], int len)
{
	get_rev(len);
	static int tmp1[Maxn], tmp2[Maxn], tmpx[Maxn], tmpy[Maxn];
	for (int i = 0; i < len; i++)
		tmpx[i] = x[i], tmpy[i] = y[i];
	multi(tmpx, tmpy, tmp1, len, p1);
	for (int i = 0; i < len; i++)
		tmpx[i] = x[i], tmpy[i] = y[i];
	multi(tmpx, tmpy, tmp2, len, p2);
	long long P1 = fast_pow(p2, p1 - 2, p1), P2 = fast_pow(p1, p2 - 2, p2);
	for (int i = 0; i < len; i++)
		result[i] = (mul(mul(tmp1[i], p2), P1) + mul(mul(tmp2[i], p1), P2)) % lcm % mod;
}
void poly_pow(int y, int len)
{
	int now = 0;
	static int ans[2][Maxn];
	memset(ans[0], 0, sizeof(int[len << 1]));
	memset(ans[1], 0, sizeof(int[len << 1]));
	static int tmp[Maxn], result[2][Maxn];
	for (int j = 30; j >= 0; j--)
	{
		cerr << j << endl;
		memset(tmp, 0, sizeof(int[len << 1]));
		for (int i = 0; i < len; i++)
			tmp[i] = (ans[0][i] + ans[1][i]) % mod;
		tmp[0]++;
		for (int p = 0; p <= 1; p++)
			multi(tmp, ans[(now & 1) ^ p], result[p], len << 1);
		for (int p = 0; p <= 1; p++)
			for (int i = 0; i < len; i++)
				(ans[p][i] += result[p][i]) %= mod;
		now <<= 1;
		if (y & (1 << j))
		{
			for (int i = len - 1; i; i--)
				(ans[1][i] += ans[0][i - 1] + ans[1][i - 1]) %= mod;
			ans[1][1]++;
			now++;
		}
		if (now != y)
			for (int i = 1; i <= maxn; i += 2)
				(tot += ans[1][i]) %= mod;
	}
}
int lower(int x)
{
	int tmp = 1;
	for (; tmp < x; tmp <<= 1);
	return tmp;
}
int main()
{
	scanf("%d%d%d", &maxn, &maxa, &mod);
	poly_pow(maxa, lower(maxn + 1));
	printf("%d", tot);
	return 0;
}

NoteIn the first example, interesting test cases look as follows: 1              1              1              32              4              6              2 4 6

Output a single integer — the number of test cases which satisfy the constraints and make both wrong solutions output an incorrect answer, modulo q.

Test data generation is not an easy task! Often, generating big random test cases is not enough to ensure thorough testing of solutions for correctness.For example, consider a problem from an old Codeforces round. Its input format looks roughly as follows:The first line contains a single integer n (1 ≤ n ≤ maxn) — the size of the set. The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ maxa) — the elements of the set in increasing order.If you don't pay attention to the problem solution, it looks fairly easy to generate a good test case for this problem. Let n = maxn, take random distinct ai from 1 to maxa, sort them... Soon you understand that it's not that easy.Here is the actual problem solution. Let g be the greatest common divisor of a1, a2, ..., an. Let x = an / g - n. Then the correct solution outputs "Alice" if x is odd, and "Bob" if x is even.Consider two wrong solutions to this problem which differ from the correct one only in the formula for calculating x.The first wrong solution calculates x as x = an / g (without subtracting n).The second wrong solution calculates x as x = an - n (without dividing by g).A test case is interesting if it makes both wrong solutions output an incorrect answer.Given maxn, maxa and q, find the number of interesting test cases satisfying the constraints, and output it modulo q.

Input: 3 6 100000

Output: 4

Input: 6 21 100129

Output: 154

Input: 58 787788 50216

Output: 46009

Input: 1 1 10000

Output: 0

Input: 1 20 100000

Output: 10

Input: 2 20 100000

Output: 10

Input: 100 12345 100000

Output: 22765

Input: 30000 1000000000 100123

Output: 21272

Input: 30000 536870911 100123

Output: 82802

Input: 200 1000000000 100123

Output: 88152

Input: 200 536870911 100123

Output: 11017

Input: 200 536870912 100123

Output: 11018

Input: 1000 536870911 100123

Output: 64128

Input: 2000 536870911 100123

Output: 95429

Input: 4000 536870911 100123

Output: 9643

Input: 8000 536870911 100123

Output: 84503

Input: 16000 536870911 100123

Output: 57489

Input: 16000 1000000000 100123

Output: 89479

Input: 10 20 100000

Output: 184

Input: 20 25 100000

Output: 698

Input: 5 5 100000

Output: 2

Input: 5 7 100000

Output: 4

Input: 1 2 100123

Output: 1

Input: 1 536870911 100123

Output: 5692

Input: 1 536870912 100123

Output: 5693

Input: 1 1000000000 100123

Output: 85861

Input: 2 1000000000 100123

Output: 85861

Input: 2 2 100123

Output: 1

Input: 2 3 100123

Output: 1

Input: 1 3 100123

Output: 1

Input: 16383 1000000000 100129

Output: 92305

Input: 16384 1000000000 100129

Output: 92305

Input: 16385 1000000000 100129

Output: 498

Input: 30000 999999999 100129

Output: 23865

Input: 30000 536870912 100129

Output: 90668

Input: 30000 536870913 100129

Output: 90668

Input: 29673 671088479 10000

Output: 67

Input: 29804 939519871 100000

Output: 99167

Input: 29097 671084543 100123

Output: 69927

Input: 29364 536805295 100128

Output: 55925

Input: 29465 805306303 100129

Output: 71904

Input: 29441 671087487 65535

Output: 40129

Input: 29923 989593598 65536

Output: 7161

Input: 29909 794820607 59049

Output: 20195

Input: 29459 805306359 46656

Output: 36813

Input: 29521 536575999 30030

Output: 13478

Input: 29327 771678207 60060

Output: 15506

Input: 29523 939524027 90090

Output: 37791

Input: 29182 804781951 10201

Output: 8309

Input: 29292 671087615 91809

Output: 62523

Input: 29939 981434367 97969

Output: 45387

Input: 29928 938995199 96721

Output: 17029

Input: 29811 803209213 97343

Output: 75766

Input: 29216 939522047 79507

Output: 7416

Input: 29622 805175167 83521

Output: 30548

Input: 29825 201293823 67228

Output: 62779

Input: 29244 922681341 78125

Output: 11842

Input: 29782 536870845 100126

Output: 74061

Input: 29725 535755775 100118

Output: 6811

Input: 29393 536870879 10001

Output: 6365

Input: 29875 805036031 10002

Output: 3441

Input: 29876 804782015 100106

Output: 35274

Input: 1 1 100000

Output: 0

Input: 1 2 100000

Output: 1

Input: 2 2 100000

Output: 1

Input: 1 3 100000

Output: 1

Input: 2 3 100000

Output: 1

Input: 3 3 100000

Output: 1

Input: 1 4 100000

Output: 2

Input: 2 4 100000

Output: 2

Input: 3 4 100000

Output: 2

Input: 4 4 100000

Output: 2

Input: 1 5 100000

Output: 2

Input: 2 5 100000

Output: 2

Input: 3 5 100000

Output: 2

Input: 4 5 100000

Output: 2

Input: 5 5 100000

Output: 2

Input: 1 6 100000

Output: 3

Input: 2 6 100000

Output: 3

Input: 3 6 100000

Output: 4

Input: 4 6 100000

Output: 4

Input: 5 6 100000

Output: 4

Input: 6 6 100000

Output: 4

Input: 1 7 100000

Output: 3

Input: 2 7 100000

Output: 3

Input: 3 7 100000

Output: 4

Input: 4 7 100000

Output: 4

Input: 5 7 100000

Output: 4

Input: 6 7 100000

Output: 4

Input: 7 7 100000

Output: 4

Input: 1 8 100000

Output: 4

Input: 2 8 100000

Output: 4

Input: 3 8 100000

Output: 5

Input: 4 8 100000

Output: 5

Input: 5 8 100000

Output: 5

Input: 6 8 100000

Output: 5

Input: 7 8 100000

Output: 5

Input: 8 8 100000

Output: 5

Input: 30000 30000 30000

Output: 13816

Input: 30000 59999 10000

Output: 1987

Input: 30000 60000 10000

Output: 5379

Input: 30000 60001 10000

Output: 5379

Input: 30000 60002 10000

Output: 66

Input: 29999 30000 10007

Output: 2397

Input: 29999 29999 29999

Output: 8411


The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≤ pb, ps, pc ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 1012) — the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

#include <iostream>

using namespace std;
string s;
int need[3] = {};
int have[3] = {};
int price[3] = {};
long long r ;
bool solve(long long m)
{
    long long p = 0;
    for (int i = 0 ; i < 3 ; i++)
    {
        long long z = m*need[i];
        z = z - have[i];
        if (z > 0)
        {
            p = p + z*price[i];
        }
    }
    return p <= r;
}

int main()
{
    cin >> s;
    for (int i = 0 ; i < s.size() ; i++)
    {
        if (s[i] == 'B')need[0]++;
        if (s[i] == 'S')need[1]++;
        if (s[i] == 'C')need[2]++;
    }
    for (int i = 0 ; i < 3 ; i++)
    {
        cin >> have[i];
    }
    for (int i = 0 ; i < 3 ; i++)
        cin >> price[i];
    cin >> r;
    long long s = 0 , e = 1e15;
    long long ans = 0;
    while(s <= e)
    {
        long long middle = (s+e)/2;
        if (solve(middle))
           {

              s = middle+1;
              ans = middle;
           }
        else e = middle - 1;

    }
    cout << ans << endl;

    return 0;
}

Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.

Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) и 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "ВSCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese.Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.

Input:
BBBSSC
6 4 1
1 2 3
4

Output: 2

Input:
BBC
1 10 1
1 10 1
21

Output: 7

Input:
BSC
1 1 1
1 1 3
1000000000000

Output: 200000000001

Input:
B
1 1 1
1 1 1
381

Output: 382

Input:
BSC
3 5 6
7 3 9
100

Output: 10

Input:
BSC
100 1 1
100 1 1
100

Output: 51

Input:
SBBCCSBB
1 50 100
31 59 21
100000

Output: 370

Input:
BBBBCCCCCCCCCCCCCCCCCCCCSSSSBBBBBBBBSS
100 100 100
1 1 1
3628800

Output: 95502

Input:
BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
10 20 40
100 100 100
200

Output: 0

Input:
BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
10 20 40
100 100 100
2000

Output: 1

Input:
BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
10 20 40
100 100 100
300

Output: 0

Input:
BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
10 20 40
100 100 100
300000000

Output: 42858

Input:
BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
10 20 40
100 100 100
914159265358

Output: 130594181

Input:
SSSSSSSSSSBBBBBBBBBCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSBB
31 53 97
13 17 31
914159265358

Output: 647421579

Input:
BBBCSBSBBSSSSCCCCBBCSBBBBSSBBBCBSCCSSCSSCSBSSSCCCCBSCSSBSSSCCCBBCCCSCBCBBCCSCCCCSBBCCBBBBCCCCCCBSSCB
91 87 17
64 44 43
958532915587

Output: 191668251

Input:
CSSCBBCCCSBSCBBBCSBBBCBSBCSCBCSCBCBSBCBCSSBBSBBCBBBBSCSBBCCBCCBCBBSBSBCSCSBBSSBBCSSBCSCSCCSSBCBBCBSB
56 34 48
78 6 96
904174875419

Output: 140968956

Input:
CCSCCCSBBBSCBSCSCCSSBBBSSBBBSBBBCBCSSBCSCBBCCCBCBCBCCCSSBSBBCCCCCBBSCBSCBCBBCBBCSSBCSBSSCCSCCSCCBBBS
33 73 67
4 56 42
886653164314

Output: 277425898

Input:
SBCSSCBBSSBCSSBBBSSBSCBSSSCBBSBBBBCSBCSBSCBSCBSCBSBSSCCCCBSBCCBCBSCCCBSCCBSBBCBSSCCCCSBSBBBSSSBCSCBC
94 16 85
14 18 91
836590091442

Output: 217522127

Input:
BSCSBSCCSCSSCCCSBCSSBCBBSCCBSCCSSSSSSSSSCCSBSCCBBCBBSBSCCCCBCSBSBSSBBBBBSSBSSCBCCSSBSSSCBBCSBBSBCCCB
67 54 8
36 73 37
782232051273

Output: 154164772

Input:
CBBCBSBCCSCBSSCCBCSBCSBBSCBBCSCCBSCCSCSBBSSBSBSCBBSBBCSSSSBBBBSBBCBCSBBCBCSSBBCSBSCCSCSBCSCBSCCBBCSC
71 71 52
52 88 3
654400055575

Output: 137826467

Input:
CBBCBSBCCSCBSSCCBCSBCSBBSCBBCSCCBSCCSCSBBSBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBCBBCSC
100 1 1
1 17 23
954400055575

Output: 1355681897

Input:
C
100 100 100
1 1 1
1000000000000

Output: 1000000000100

Input:
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
100 100 100
100 100 100
1000000000000

Output: 100000001

Input:
B
100 100 100
1 1 1
1

Output: 101

Input:
SC
2 1 1
1 1 1
100000000000

Output: 50000000001

Input:
B
100 1 1
1 1 1
1000000000000

Output: 1000000000100

Input:
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
1 1 1
100 100 100
1000000000000

Output: 100000000

Input:
CC
1 1 1
100 100 100
1

Output: 0

Input:
B
100 100 100
1 1 1
1000000000000

Output: 1000000000100

Input:
BSC
100 100 100
1 1 1
1000000000000

Output: 333333333433

Input:
BSC
100 100 100
1 1 1
1

Output: 100

[{'input': 'CSSCBBCCCSBSCBBBCSBBBCBSBCSCBCSCBCBSBCBCSSBBSBBCBBBBSCSBBCCBCCBCBBSBSBCSCSBBSSBBCSSBCSCSCCSSBCBBCBSB\r\n56 34 48\r\n78 6 96\r\n904174875419\r\n', 'output': ['140968956']}, {'input': 'BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\r\n10 20 40\r\n100 100 100\r\n2000\r\n', 'output': ['1']}, {'input': 'BSC\r\n3 5 6\r\n7 3 9\r\n100\r\n', 'output': ['10']}, {'input': 'SC\r\n2 1 1\r\n1 1 1\r\n100000000000\r\n', 'output': ['50000000001']}, {'input': 'C\r\n100 100 100\r\n1 1 1\r\n1000000000000\r\n', 'output': ['1000000000100']}]

[{'input': 'BBBCSBSBBSSSSCCCCBBCSBBBBSSBBBCBSCCSSCSSCSBSSSCCCCBSCSSBSSSCCCBBCCCSCBCBBCCSCCCCSBBCCBBBBCCCCCCBSSCB\r\n91 87 17\r\n64 44 43\r\n958532915587\r\n', 'output': ['191668251']}, {'input': 'B\r\n100 100 100\r\n1 1 1\r\n1\r\n', 'output': ['101']}, {'input': 'SSSSSSSSSSBBBBBBBBBCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSBB\r\n31 53 97\r\n13 17 31\r\n914159265358\r\n', 'output': ['647421579']}, {'input': 'CSSCBBCCCSBSCBBBCSBBBCBSBCSCBCSCBCBSBCBCSSBBSBBCBBBBSCSBBCCBCCBCBBSBSBCSCSBBSSBBCSSBCSCSCCSSBCBBCBSB\r\n56 34 48\r\n78 6 96\r\n904174875419\r\n', 'output': ['140968956']}, {'input': 'BSC\r\n100 1 1\r\n100 1 1\r\n100\r\n', 'output': ['51']}]

[{'input': 'B\r\n100 1 1\r\n1 1 1\r\n1000000000000\r\n', 'output': ['1000000000100']}, {'input': 'CBBCBSBCCSCBSSCCBCSBCSBBSCBBCSCCBSCCSCSBBSSBSBSCBBSBBCSSSSBBBBSBBCBCSBBCBCSSBBCSBSCCSCSBCSCBSCCBBCSC\r\n71 71 52\r\n52 88 3\r\n654400055575\r\n', 'output': ['137826467']}, {'input': 'BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\r\n10 20 40\r\n100 100 100\r\n2000\r\n', 'output': ['1']}, {'input': 'B\r\n100 100 100\r\n1 1 1\r\n1000000000000\r\n', 'output': ['1000000000100']}, {'input': 'CSSCBBCCCSBSCBBBCSBBBCBSBCSCBCSCBCBSBCBCSSBBSBBCBBBBSCSBBCCBCCBCBBSBSBCSCSBBSSBBCSSBCSCSCCSSBCBBCBSB\r\n56 34 48\r\n78 6 96\r\n904174875419\r\n', 'output': ['140968956']}]

[{'input': 'CBBCBSBCCSCBSSCCBCSBCSBBSCBBCSCCBSCCSCSBBSBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBCBBCSC\r\n100 1 1\r\n1 17 23\r\n954400055575\r\n', 'output': ['1355681897']}, {'input': 'BBBBCCCCCCCCCCCCCCCCCCCCSSSSBBBBBBBBSS\r\n100 100 100\r\n1 1 1\r\n3628800\r\n', 'output': ['95502']}, {'input': 'CSSCBBCCCSBSCBBBCSBBBCBSBCSCBCSCBCBSBCBCSSBBSBBCBBBBSCSBBCCBCCBCBBSBSBCSCSBBSSBBCSSBCSCSCCSSBCBBCBSB\r\n56 34 48\r\n78 6 96\r\n904174875419\r\n', 'output': ['140968956']}, {'input': 'C\r\n100 100 100\r\n1 1 1\r\n1000000000000\r\n', 'output': ['1000000000100']}, {'input': 'BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\r\n10 20 40\r\n100 100 100\r\n2000\r\n', 'output': ['1']}]


The first line of the input contains the string $$$s$$$ consisting of $$$|s|$$$ ($$$1\leq |s|\leq 100$$$) lowercase Latin letters.

#include<bits/stdc++.h>
using namespace std;
string st;
bool pd(int i)
{
    return st[i]=='a'||st[i]=='e'||st[i]=='i'||st[i]=='o'||st[i]=='u';
}
int main()
{
	cin>>st;
	for(int i=0;i<st.size();i++)
	{
		if(!pd(i)&&!pd(i+1)&&st[i]!='n')
		{
			cout<<"NO"<<endl;
			return 0;
		}
	}
	cout<<"YES"<<endl;
	return 0;
}

NoteIn the first and second samples, a vowel goes after each consonant except "n", so the word is Berlanese.In the third sample, the consonant "c" goes after the consonant "r", and the consonant "s" stands on the end, so the word is not Berlanese.

Print "YES" (without quotes) if there is a vowel after every consonant except "n", otherwise print "NO". You can print each letter in any case (upper or lower).

Vitya has just started learning Berlanese language. It is known that Berlanese uses the Latin alphabet. Vowel letters are "a", "o", "u", "i", and "e". Other letters are consonant.In Berlanese, there has to be a vowel after every consonant, but there can be any letter after any vowel. The only exception is a consonant "n"; after this letter, there can be any letter (not only a vowel) or there can be no letter at all. For example, the words "harakiri", "yupie", "man", and "nbo" are Berlanese while the words "horse", "king", "my", and "nz" are not.Help Vitya find out if a word $$$s$$$ is Berlanese.

Input: sumimasen

Output: YES

Input: ninja

Output: YES

Input: codeforces

Output: NO

Input: auuaoonntanonnuewannnnpuuinniwoonennyolonnnvienonpoujinndinunnenannmuveoiuuhikucuziuhunnnmunzancenen

Output: YES

Input: n

Output: YES

Input: necnei

Output: NO

Input: nternn

Output: NO

Input: aucunuohja

Output: NO

Input: a

Output: YES

Input: b

Output: NO

Input: nn

Output: YES

Input: nnnzaaa

Output: YES

Input: zn

Output: NO

Input: ab

Output: NO

Input: aaaaaaaaaa

Output: YES

Input: aaaaaaaaab

Output: NO

Input: aaaaaaaaan

Output: YES

Input: baaaaaaaaa

Output: YES

Input: naaaaaaaaa

Output: YES

Input: nbaaaaaaaa

Output: YES

Input: bbaaaaaaaa

Output: NO

Input: bnaaaaaaaa

Output: NO

Input: eonwonojannonnufimiiniewuqaienokacevecinfuqihatenhunliquuyebayiaenifuexuanenuaounnboancaeowonu

Output: YES

Input: uixinnepnlinqaingieianndeakuniooudidonnnqeaituioeneiroionxuowudiooonayenfeonuino

Output: NO

Input: nnnnnyigaveteononnnnxaalenxuiiwannntoxonyoqonlejuoxuoconnnentoinnul

Output: NO

Input: ndonneasoiunhomuunnhuitonnntunntoanerekonoupunanuauenu

Output: YES

Input: anujemogawautiedoneobninnibonuunaoennnyoorufonxionntinimiboonununnnnnleenqunminzayoutanlalo

Output: NO

Input: y

Output: NO

Input: by

Output: NO

Input: yy

Output: NO

Input: nbn

Output: NO

Input: nz

Output: NO

Input: king

Output: NO

Input: g

Output: NO

Input: az

Output: NO

Input: x

Output: NO

Input: z

Output: NO

Input: yn

Output: NO

Input: aeo

Output: YES

Input: nb

Output: NO

Input: npn

Output: NO

Input: kini

Output: YES

Input: pya

Output: NO

Input: m

Output: NO

Input: p

Output: NO

Input: aaaaaaaak

Output: NO

Input: aab

Output: NO

Input: d

Output: NO

Input: at

Output: NO

Input: aaaaaak

Output: NO

Input: aaz

Output: NO

Input: aaab

Output: NO

Input: s

Output: NO

Input: nzzen

Output: NO

Input: aeionnhhhn

Output: NO

Input: h

Output: NO

Input: ny

Output: NO

[{'input': 'nn\r\n', 'output': ['YES']}, {'input': 'nternn\r\n', 'output': ['NO']}, {'input': 'aaz\r\n', 'output': ['NO']}, {'input': 'naaaaaaaaa\r\n', 'output': ['YES']}, {'input': 'uixinnepnlinqaingieianndeakuniooudidonnnqeaituioeneiroionxuowudiooonayenfeonuino\r\n', 'output': ['NO']}]

[{'input': 'aeo\r\n', 'output': ['YES']}, {'input': 'nternn\r\n', 'output': ['NO']}, {'input': 'aaaaaak\r\n', 'output': ['NO']}, {'input': 'ninja\r\n', 'output': ['YES']}, {'input': 'kini\r\n', 'output': ['YES']}]

[{'input': 'nbaaaaaaaa\r\n', 'output': ['YES']}, {'input': 'ndonneasoiunhomuunnhuitonnntunntoanerekonoupunanuauenu\r\n', 'output': ['YES']}, {'input': 'ninja\r\n', 'output': ['YES']}, {'input': 'necnei\r\n', 'output': ['NO']}, {'input': 'yn\r\n', 'output': ['NO']}]

[{'input': 'aucunuohja\r\n', 'output': ['NO']}, {'input': 'ninja\r\n', 'output': ['YES']}, {'input': 'anujemogawautiedoneobninnibonuunaoennnyoorufonxionntinimiboonununnnnnleenqunminzayoutanlalo\r\n', 'output': ['NO']}, {'input': 'codeforces\r\n', 'output': ['NO']}, {'input': 'b\r\n', 'output': ['NO']}]