E. Journey
time limit per test
5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output
Example
Input
7 5
25 6 3
20 2 5
7 3 1
0 2
6 0
WWLDL
WWWWL
WWWWL
WWWWL
WWWLL
WWWWW
DWLLW
Output
150