Under construction

Still being built. Things may change

Gahui and Music

Time limit1sMemory limit512 MB

Summary
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 nn measures, the performers stop playing once both of the following conditions hold.

  • They have played the nn-th measure.
  • The effect of a Dal Segno has not been triggered.

Gahui wants to place at most 22 Segno or Dal Segno marks at suitable positions so that the audience's satisfaction is maximized. Also, there must be no part that repeats 33 or more times. Please help Gahui.

Input

The first line contains the number of measures nn.

The second line contains the information for each measure, separated by spaces. The ii-th item describes the ii-th measure. Each item is given in one of 5 formats.

  • A number
    • The audience's satisfaction when the ii-th measure is played. It is an integer between −500 000-500\,000 and 500 000500\,000, inclusive.
  • S
    • A position where a Segno can be placed, given at the end bar line of the ii-th measure.
  • DS
    • A position where a Dal Segno can be placed, given at the end bar line of the ii-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 ii-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 ii-th measure.

Output

Print the answer to the problem.

Constraints

  • 1≤n≤500 0001 \le n \le 500\,000

Hint

But if a song is to Gahui's taste, you can listen to it many times.

Examples2

  1. Example 1

    Input
    6
    2 3 S DS or S -2 DS or S
    
    Expected output
    8
    
  2. Example 2

    Input
    1
    2
    
    Expected output
    2