Problem:
Score : 300 points

Problem Statement

AtCoDeer the deer and his friend TopCoDeer is playing a game.
The game consists of N turns.
In each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition:

(※) After each turn, (the number of times the player has played Paper)≦(the number of times the player has played Rock).

Each player's score is calculated by (the number of turns where the player wins) - (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.

(For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)

With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the N turns, before the game starts.
Plan AtCoDeer's gesture in each turn to maximize AtCoDeer's score.

The gesture that TopCoDeer will play in each turn is given by a string s. If the i-th (1≦i≦N) character in s is g, TopCoDeer will play Rock in the i-th turn. Similarly, if the i-th (1≦i≦N) character of s in p, TopCoDeer will play Paper in the i-th turn.

Constraints

1≦N≦10^5

N=|s|

Each character in s is g or p.

The gestures represented by s satisfy the condition (※).

Input

The input is given from Standard Input in the following format:

s

Output

Print the AtCoDeer's maximum possible score.

Sample Input 1

gpg

Sample Output 1

0

Playing the same gesture as the opponent in each turn results in the score of 0, which is the maximum possible score.

Sample Input 2

ggppgggpgg

Sample Output 2

2

For example, consider playing gestures in the following order: Rock, Paper, Rock, Paper, Rock, Rock, Paper, Paper, Rock, Paper. This strategy earns three victories and suffers one defeat, resulting in the score of 2, which is the maximum possible score.

Solution in Rust (1.15.1) (judge verdict: Time Limit Exceeded)

struct State {
    g: i32,
    p: i32,
    score: i32
}

use std::cmp::max;
fn main() {
    let stdin = std::io::stdin();
    let s = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()))
            .by_ref()
            .map(|r|r.unwrap() as char)
            .skip_while(|c|c.is_whitespace())
            .take_while(|c|!c.is_whitespace())
            .collect::<Vec<_>>();
    
    let mut states = Vec::new();
    states.push(State { g:0, p:0, score:0 });
    let is_g = |c| if c == 'g' { 1 } else { 0 };
    let is_p = |c| if c == 'p' { 1 } else { 0 };
    for c in s {
        let mut next = Vec::new();
        for x in states.iter() {
            if x.p < x.g {
                next.push(State {g:x.g, p:x.p + 1, score:x.score + is_g(c)});
            }
            next.push(State {g:x.g + 1, p:x.p, score:x.score - is_p(c)});
        }
        states = next;
    }

    let result = states.iter().fold(0, |v, x| max(v, x.score));
    println!("{}", result);
}