logo AlgoBeat OnlineJudge
登录 注册

#217815. [JAG 2026 Summer Camp #3] Treasure Hunting

内存限制:2048 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are playing a treasure-hunting video game that takes place on a two-dimensional plane.

You have a string of length consisting only of R, U, and ?. First, you replace each occurrence of ? in with either R or U.

After replacing all occurrences of ?, you start at and make moves according to . Let denote your current location. On the -th move, if the -th character of is R, your next location is ; if it is U, your next location is .

There are treasure boxes on the plane. The -th treasure box is located at , and if you reach this location, you can open the box to collect coins. Multiple treasure boxes may be located at the same point. If you reach such a point, you can open all the treasure boxes there and collect the coins from each of them.

Find the maximum number of coins you can collect if you optimally replace each occurrence of ? in .

输入格式

The input consists of a single test case of the following format.

m n
s
a_1 b_1 c_1
a_2 b_2 c_2
...
a_n b_n c_n

The first line contains two integers and . is the total number of moves in this game () and is the number of treasure boxes ().

The second line contains a string of length . Each character of is either R, U, or ?.

Each of the next lines contains three integers , , and . is the location of the -th treasure box (, , ). is the number of coins in the box ().

输出格式

Output the maximum number of coins you can collect.

样例

样例输入 1

8 5
R?UU?U?R
1 2 3
2 3 5
1 6 1
3 5 4
5 3 10

样例输出 1

12

样例输入 2

10 2
RRRRUUUURR
5 0 10
0 1 8

样例输出 2

0

样例输入 3

20 15
?RU?R?????U?R?UR???U
1 7 19
16 4 13
1 0 15
8 3 20
7 8 18
2 10 17
5 9 15
14 6 16
0 1 6
11 1 19
10 2 14
12 3 11
4 11 18
2 3 16
2 16 11

样例输出 3

57

数据范围与提示

In Sample Input 1, one optimal replacement results in the string RUUURUUR. With this string, you can open the first, second, and fourth treasure boxes and collect coins.