Gahui and Music
Time limit1sMemory limit512 MB
Given n measures with satisfaction values and optional Segno or Dal Segno marks, find the maximum total satisfaction when at most two marks are placed and no part repeats three or more times.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Gahui wrote a song. When the performers reach a Dal Segno, they act as follows.
- If there is a Segno before the Dal Segno, they return to the Segno and play from that position.
- If there is no Segno before the Dal Segno, they return to the beginning and play from the first measure.
Each Dal Segno takes effect only once. When the song has measures, the performers stop playing once both of the following conditions hold.
- They have played the -th measure.
- The effect of a Dal Segno has not been triggered.
Gahui wants to place at most Segno or Dal Segno marks at suitable positions so that the audience's satisfaction is maximized. Also, there must be no part that repeats or more times. Please help Gahui.
Input
The first line contains the number of measures .
The second line contains the information for each measure, separated by spaces. The -th item describes the -th measure. Each item is given in one of 5 formats.
- A number
- The audience's satisfaction when the -th measure is played. It is an integer between and , inclusive.
S- A position where a Segno can be placed, given at the end bar line of the -th measure.
DS- A position where a Dal Segno can be placed, given at the end bar line of the -th measure.
S or DS- A position where either a Segno or a Dal Segno can be placed, given at the end bar line of the -th measure.
DS or S- A position where either a Dal Segno or a Segno can be placed, given at the end bar line of the -th measure.
Output
Print the answer to the problem.
Constraints
Hint
But if a song is to Gahui's taste, you can listen to it many times.