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.



Sample input:
gpg


Sample output:
0

Submission status: Accepted

Solution code:
n = input()

point = 0
cnt = 0
for h in n:
    if cnt <= 0:
        cnt += 1
        if h == 'p':
            point -= 1
    elif h == 'g':
        cnt -= 1
        point += 1
    else:
        cnt -= 1
print(point)

Submission status: Accepted

Solution code:
s = input()

P = 0
for c in s:
  if c == 'p':
    P += 1

print(len(s)//2 - P)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    s = input()
    ans = 0
    for i in range(len(s)):
        if i < (len(s) + 2 - 1) // 2:
            ans -= s[i] == "p"
        else:
            ans += s[i] == "g"
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    s = input()
    ans = 0
    for i in range(len(s)):
        if i < (len(s) + 2 - 1) // 2:
            if s[i] == "p":
                ans -= 1
        else:
            if s[i] == "g":
                ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
ans = 0
for i, c in enumerate(s):
    if i%2==0 and c=='p':
        ans -= 1
    elif i%2==1 and c=='g':
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
s = raw_input()
N = len(s)

def calc_score(hands):
    score = 0
    for i in range(N):
        if s[i] == 'g' and hands[i] == 'p':
            score += 1
        elif s[i] == 'p' and hands[i] == 'g':
            score -= 1
    return score

hands = 'gp' * (N/2) + 'g' * (N%2)
score = calc_score(hands)
print score

Submission status: Accepted

Solution code:
s = raw_input()
N = len(s)
ans = 0
T = 0
for i in range(N):
    if s[i] == 'p':
        if T > 0:
            T -= 1
        else:
            T += 1
            ans -= 1
    else: # 'g'
        if T > 0:
            T -= 1
            ans += 1
        else:
            T += 1
print ans

Submission status: Accepted

Solution code:
import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    s = [1 if hand == 'p' else 0 for hand in input()]
    n = len(s)
    t = [1 if i % 2 else 0 for i in range(n)]
    ans = 0

    for h1, h2 in zip(s, t):
        if h1 < h2:
            ans += 1
        elif h2 < h1:
            ans -= 1
        else:
            pass

    assert ans >= 0
    
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
s = input()
n = len(s)

gnum = s[1:].count("g")
pnum = n - gnum - 1
# print(gnum)
# print(pnum)
print((gnum - pnum + 1) // 2)

Submission status: Accepted

Solution code:
s = input()
Ng = 0
Np = 0
Nw = 0
Nl = 0
for i in s:
    if Ng > Np:
        if i == 'g':
            Nw += 1
        Np += 1
    else:
        if i == 'p':
            Nl += 1
        Ng += 1

print(Nw - Nl)

Submission status: Accepted

Solution code:
S = input().strip()
N = len(S)
gn = len([1 for i in range(N) if S[i]=='g'])
gm = int((N+1)/2)
print(gn-gm)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

import sys

def main():
    s = raw_input()
    num_g = sum(1 for c in s if c == 'g')
    num_p = sum(1 for c in s if c == 'p')
    print (num_g-num_p)/2

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
score=0

for i in range(len(s)):
    if i%2==0:
        if s[i]=="p":
            score-=1
    else:
        if s[i]=="g":
            score+=1

print(score)

Submission status: Accepted

Solution code:
S = raw_input()

cnt = 0

for i in S:
	if i == "p":
		cnt += 1

print len(S) // 2 - cnt

Submission status: Accepted

Solution code:
s = input()

t = int(len(s)/2)
ans_left = 'g' * (len(s)-t)
ans_right = 'p' * t
ans = ans_left + ans_right
ret = 0

for i in range(len(s)-t):
    if(s[i:i+1] == 'p'):
        ret -= 1

for i in range(len(s)-t, len(s)):
    if(s[i:i+1] == 'g'):
        ret += 1

print(ret)

Submission status: Accepted

Solution code:
import sys
import math

s = sys.stdin.readline()
print int(math.floor((len(s) - 1) / 2.0) - s.count('p'))

Submission status: Accepted

Solution code:
opponent = input()
count_p = 0
count_g = 0
point = 0
for i in range(len(opponent)):
    if opponent[i] == 'g':
        if count_p < count_g:
            count_p += 1
            point += 1
        else:
            count_g += 1
    if opponent[i] == 'p':
        if count_p < count_g:
            count_p += 1
        else:
            count_g += 1
            point -= 1
print(point)

Submission status: Accepted

Solution code:
l = input()
gcnt, pcnt, wcnt, lcnt = 0,0,0,0

for i in range(len(l)):
	if pcnt + 1 <= gcnt: #show "p"
		pcnt += 1
		if l[i] == 'g':
			wcnt += 1
	else: #show "g"
		gcnt += 1
		if l[i] == 'p':
			lcnt += 1

print(wcnt - lcnt)

Submission status: Accepted

Solution code:
def count(a, t):
    ans = 0
    for i in range(len(t)):
        if a[i] == t[i]:
            ans += 0
        elif a[i] == "g" and t[i] == "p":
            ans -= 1
        elif a[i] == "p" and t[i] == "g":
            ans += 1
    return ans


def main():
    s = input()
    a = "gp" * (len(s) // 2 + 1)
    print(count(a, s))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#abc046_d

s=raw_input()

gu=s.count('g')
pa=s.count('p')

res=(gu-pa)/2
print res

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

s = str(input())
n = len(s)

G = s.count('g')
P = s.count('p')

ans = (G - P) // 2

print(ans)

Submission status: Accepted

Solution code:
s=raw_input()

p=0

for i in range(len(s)):
    if s[i] == 'p':
        p += 1

print (len(s)//2 - p)

Submission status: Accepted

Solution code:
from collections import Counter
s =Counter(input())
print((s['g']-s['p'])//2)

Submission status: Accepted

Solution code:
s=raw_input()

g=0
p=0

for i in range(len(s)):
   if s[i]=='g':
       g+=1
   else:
       p+=1

sum=g+p
print sum/2-p

Submission status: Accepted

Solution code:
s = str(input())
gn = int(s.count("g"))
pn = int(s.count("p"))
print((gn-pn)//2)

Submission status: Accepted

Solution code:
T = input()
count = 0
point = 0
for t in T:
    if count % 2 == 0:
        a = 'g'
    else:
        a = 'p'
    if a=='p' and t=='g':
        point += 1
    elif a=='g' and t=='p':
        point -= 1
    else:
        point += 0
    count += 1
print(point)

Submission status: Accepted

Solution code:
pt = raw_input()
g = p = 0
score = 0
for c in pt:
    t = "g"
    if g > p:
        t = "p"
    if t == "p": p += 1
    if t == "g": g += 1
    if c == "g" and t == "p": score += 1
    if c == "p" and t == "g": score -= 1

print score

Submission status: Accepted

Solution code:
T = input()
A = ["gp"[i%2] for i in range(len(T))]
A = "".join(A)

point = 0
for t,a in zip(T,A):
    if t == "g" and a == "p":
        point += 1
    elif t == "p" and a == "g":
        point -= 1

print(point)

Submission status: Accepted

Solution code:
# D
from collections import Counter
s = Counter(input())
print((s['g']-s['p'])//2)

Submission status: Accepted

Solution code:
try:
    N = raw_input()
    print N.count('g') + (-len(N)/2)

except EOFError:
    pass

Submission status: Accepted

Solution code:
import sys

s = sys.stdin.readline().strip()

r = 0
for i, v in enumerate(s):
    vv = 'p' if (i & 1) else 'g'
    if vv != v:
        if vv == 'p':
            r += 1
        else:
            r -= 1
print r

Submission status: Accepted

Solution code:
def read_input():
    return list(input())


def maximize_point(against):
    """Calc max point.

    >>> maximize_point(list("gpg"))
    0
    >>> maximize_point(list("ggppgggpgg"))
    2
    """
    cash = 0
    point = 0
    for hand in against:
        if hand == "g":
            if cash > 0:
                point += 1
                cash += -1
                # print("p")
            else:
                point += 0
                cash += 1
                # print("g")

        if hand == "p":
            if cash > 0:
                point += 0
                cash += -1
                # print("p")
            else:
                point += -1
                cash += 1
                # print("g")

    return point

if __name__ == "__main__":
    # import doctest

    # doctest.testmod()

    print(maximize_point(read_input()))

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
counter = Counter(s)
print(len(s)//2 - counter['p'])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = raw_input()

print len(s) / 2 - s.count('p')

Submission status: Accepted

Solution code:
s = raw_input()
N = len(s)
print N / 2 - s.count('p')

Submission status: Accepted

Solution code:
s = input()
c_g = 0
c_p = 0
win = 0
lose = 0
g_ = len(list(filter(lambda x:x=="g",s)))
p_ = len(list(filter(lambda x:x=="p",s)))
print((g_-p_)//2)

Submission status: Accepted

Solution code:
s = input()

if len(s) % 2 == 0:
    zenhan = s[:len(s)//2]
    kohan = s[len(s)//2:]
else:
    zenhan = s[:len(s)//2+1]
    kohan = s[len(s)//2+1:]

point = 0
for c in zenhan:
    if c == 'p':
        point -= 1

for c in kohan:
    if c == 'g':
        point += 1

print(point)

Submission status: Accepted

Solution code:
from sys import stdin
a = stdin.readline().strip()
ans = 0
cur = 0
res = ''
for i in a:
 if i=='p':
  if cur:
   res += "p"
   cur-=1
  else:
   res += "g"
   cur += 1
   ans -=1
 else:
  if cur:
   res += 'p'
   ans += 1
   cur -= 1
  else:
   res += 'g'
   cur += 1

print ans

Submission status: Accepted

Solution code:
import sys
hands = raw_input().rstrip()

score = 0
for i, hand in enumerate(hands):
    if i % 2 == 0:
        if hand == 'p':
            score -=1
    else:
        if hand == 'g':
            score += 1

print score

Submission status: Accepted

Solution code:
s = raw_input()
print len(s)/2 - s.count("p")

Submission status: Accepted

Solution code:
# coding: utf-8

inp = list(input())
count = 0

for i, t  in enumerate(inp):
    if i%2==0:#myg
        if t=='p':
            count -= 1
    else:#myp
        if t=='g':
            count += 1

print(count)

Submission status: Accepted

Solution code:
s=input()
g=p=0
for c in s:
    if c=='g':
        g+=1
    else:
        p+=1
if (g+p)%2==0:
    print((g-p)//2)
else:
    print((g-p-1)//2)

Submission status: Accepted

Solution code:
s = input()
print(len(s) // 2 - len([1 for c in s if c == 'p']))

Submission status: Accepted

Solution code:
s=input();n=len(s)//2;m=s.count('p');print(n-m);

Submission status: Accepted

Solution code:
s=input();n=len(s)//2;m=s.count('p');print(n-m);

Submission status: Accepted

Solution code:
s=raw_input()
print(len(s)-s.count("p")*2)/2

Submission status: Accepted

Solution code:
tcd = raw_input()
score = 0
for k, c in enumerate(tcd):
    if k % 2 == 1:
        if c == 'g':
            score += 1
            #print(k, 'plus')
    if k % 2 == 0:
        if c == 'p':
            score += -1
            #print(k, 'minu')
print(score)

Submission status: Accepted

Solution code:
s = list(str(input()))
ans = []
score = 0
for i in range(len(s)):
    if i % 2 == 0:
        ans.append('g')
    else:
        ans.append('p')
for i in range(len(s)):
    if s[i] == 'p' and ans[i] == 'g':
        score -= 1
    elif s[i] == 'g' and ans[i] == 'p':
        score +=1
print(score)

Submission status: Accepted

Solution code:
s=raw_input()
ans=0
for a,i in enumerate(s):
  if a%2==0 and i=='p':
    ans-=1
  elif a%2==1 and i=='g':
    ans+=1
print ans

Submission status: Accepted

Solution code:
from collections import Counter

gp = Counter(input())
n = sum(gp.values())

print(n // 2 - gp["p"])

Submission status: Accepted

Solution code:
s = raw_input()
s_length = len(s)

score = 0
for i in range(s_length):
    if s[i]=='g':
        score+=1
score -= s_length/2 if s_length%2==0 else s_length/2+1
print score

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

s = raw_input()
N = len(s)
g, p = 0, 0
ans = 0
for si in s:
    if si == "g":
        if g > p:
            ans += 1
            p += 1
        else:
            g += 1
    else:
        if g > p:
            p += 1
        else:
            ans -= 1
            g += 1
print ans

Submission status: Accepted

Solution code:
gp=raw_input()
gp_list=list(gp)

g=0
p=0
score=0

for i in gp_list:
	if i=="g":
		if p<g:
			p+=1
			score+=1
		else:
			g+=1
	else:
		if p<g:
			p+=1
		else:
			g+=1
			score-=1

print score

Submission status: Accepted

Solution code:
def judge(my, another):
    if my == another:
        return 0
    elif my == 'g':
        return -1
    else:
        return 1

s = input()

g, p = 0, 0
point = 0
for c in s:
    if p >= g:
        #print("自分の手はグー")
        point += judge('g', c)
        g += 1
    else:
        #print("自由に選べる")
        if c == 'g':
            point += 1
            p += 1
        else:
            p += 1

        
print(point)

Submission status: Accepted

Solution code:
#coding=UTF-8
#果たして鹿はじゃんけんが出来るのか

S=input()
offsets=0
for moji in S:
    if moji=='p':
        offsets=offsets+1

nagasa=len(S)

ans=nagasa//2 - offsets
print(ans)

Submission status: Accepted

Solution code:
s = input()
g = 0
p = 0
for i in s:
    if i == 'g':
        g += 1
    if i == 'p':
        p += 1
print((g - p) // 2)

Submission status: Accepted

Solution code:
S = list(raw_input())
t = 0
score = 0

for s in S:
    if s == 'g':
        if t > 0:
            t-=1
            score+=1
        else:
            t+=1
    else:
        if t > 0:
            t-=1
        else:
            t+=1
            score-=1
print score

Submission status: Accepted

Solution code:
hand = raw_input()
hands = [_ for _ in hand]
g = 0
p = 0
point = 0
for _ in range(len(hand)):
	if _%2 == 0:
		if hands[_] == 'p':
			point -= 1
	else:
		if hands[_] == 'g':
			point += 1

print point

Submission status: Accepted

Solution code:
s = input()
n = len(s)
gamedict = dict()
gamedict['p'] = 1
gamedict['g'] = 0
score = 0
for i in range(n):
    if i%2 == 0:
        mymove = 0
    else:
        mymove = 1
    score = score + (mymove - gamedict[s[i]])
print(score)

Submission status: Accepted

Solution code:
s = raw_input()

p  = s.count("p")
g = s.count("g")
if p < g:
    ans = (p + g) // 2 - p
elif p == g:
    ans = 0
else:
    ans =  - (p + g) // 2 + g
print ans,

Submission status: Accepted

Solution code:
st = input()
N = len(st)
gc = 0
ret = 0
for s in st:
    if s == 'g':
        gc += 1
    else:
        gc -= 1
for s in reversed(st):
    if s == 'g':
        gc -= 1
        if gc > 0:
            gc -= 1
            ret += 1
                        
print(ret)

Submission status: Accepted

Solution code:
s = raw_input()
s = [i for i in s]

over_g = 0

score = 0

#print s

for c in s:
    if(over_g == 0):
        if(c == 'p'):
            score -= 1
        over_g += 1
#        print over_g,'g'
    else:
        if(c == 'g'):
            score += 1
        over_g -= 1
#        print over_g,'p'

print score

Submission status: Accepted

Solution code:
s = raw_input().strip()
score = 0
for i, si in enumerate(s):
  if i%2 == 1:
    if si == 'g':
      score += 1
  else:
    if si == 'p':
      score -= 1
print score

Submission status: Accepted

Solution code:
s = raw_input()
N = len(s)
p = 0

for char in range(N):
    if s[char] == "p":
        p += 1

answer = (N // 2) - p
if answer < 0:
    print 0
else:
    print answer

Submission status: Accepted

Solution code:
l = input()
g = list(filter(lambda s: s == "g", l))
p = list(filter(lambda s: s == "p", l))

print(int((len(g) - len(p)) / 2))

Submission status: Accepted

Solution code:
import bisect
import sys
import math
import itertools
sys.setrecursionlimit(10000)

INF = float('inf')

# input macro
def i():
    return int(raw_input())
def ii():
    return map(int,raw_input().split(" "))
def s():
    return raw_input()
def ss():
    return raw_input().split(" ")
def slist():
    return list(raw_input())
#

def join(s):
    return ''.join(s)

#iterate macro
def piter(n,m):
    return itertools.permutations(n,m)
def citer(n,m):
    return itertools.combinations(n,m)

#modulo macro
def modc(a,b,m):
    c = 1
    for i in xrange(b):
        c = c * (a - i) % m
        c = c * modinv(i + 1,m) % m
    return c
 
def gcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
 
def modinv(a, m):
    (inv, q, gcd_val) = gcd(a, m)
    return inv % m

#bisect macro
def index(a, x):
    #Locate the leftmost value exactly equal to x
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    return -1

#memoize macro
def memoize(f):
    cache = {}
    def helper(*args):
        if args not in cache:
            cache[(args)] = f(*args)
        return cache[args]
    return helper

@memoize
def nck(a,b,m):
    b = min([a-b,b])
    if (b>a or b<0 or a<0):
        return 0
    elif a == 0:
        return 1
    return (nck(a-1,b-1,m)+nck(a-1,b,m)) % m

nh=zip([1,0,-1,0],[0,1,0,-1])

###########

s=slist()
g=s.count('g')
p=s.count('p')
print (g-p)/2

Submission status: Accepted

Solution code:
moveList = raw_input()

rockCounter = 0
score = 0

print (len([x for x in moveList if x == 'g']) 
       - len([x for x in moveList if x == 'p']))/2

Submission status: Accepted

Solution code:
moveList = raw_input()
print (len([x for x in moveList if x == 'g']) 
       - len([x for x in moveList if x == 'p']))/2

Submission status: Accepted

Solution code:
from sys import stdin

s = stdin.readline().strip()

points = 0

rocks, papers = 0, 0

for move in s:
    if move == 'g':
        if papers < rocks:
            points += 1
            papers += 1
        else:
            rocks += 1
    else:
        if papers < rocks:
            papers += 1
        else:
            rocks += 1
            points -= 1

print(points)

Submission status: Accepted

Solution code:
from random import random
import math
import re
import fractions

score = 0
count = 0
S = raw_input()
l = len(S)/2
cp = S.count("p")
print l - cp

Submission status: Accepted

Solution code:
moveList = raw_input()

rockCounter = 0
score = 0

for i in moveList:
    if i == 'g' and rockCounter > 0:
        score += 1
        rockCounter -= 1
    elif i == 'g':
        rockCounter += 1
    elif i == 'p' and rockCounter > 0:
        rockCounter -= 1
    else:
        rockCounter += 1
        score -= 1
        
print(score)

Submission status: Accepted

Solution code:
s = raw_input()
t = "g"*((len(s)+1)/2)+"p"*(len(s)/2)
ans = 0
for i in range(len(s)):
    if s[i]=="g" and t[i]=="p":
        ans += 1
    if s[i]=="p" and t[i]=="g":
        ans -= 1
print ans

Submission status: Accepted

Solution code:
s=raw_input()
g=p=0
#n,k=map(int,raw_input().split())
#l=map(int,raw_input().split())
ans=chk=0
for i in s:
    if i=='g' and g>p:
        ans+=1
        p+=1
    elif i=='g':
        g+=1
    if i=='p' and g>p:
        p+=1
    elif i=='p':
        g+=1
        ans-=1
print ans

Submission status: Runtime Error

Solution code:
s = input()
n = len(s)
p = 0
for e in s:
  if e == 'p':
    p += 1
print(s//2 - p)

Submission status: Runtime Error

Solution code:
def dfs(i,gu,pa,kati,make):
    global ans
    if i==len(s):
        ans.append(kati-make)
        return
    else:
        if pa==gu:
            if s[i]=='g':
                dfs(i+1,gu+1,pa,kati,make)
            else:
                dfs(i+1,gu+1,pa,kati,make+1)
        else:
            if s[i]=='g':
                dfs(i+1,gu+1,pa,kati,make)
                dfs(i+1,gu,pa+1,kati+1,make)
            else:
                dfs(i+1,gu+1,pa,kati,make+1)
                dfs(i+1,gu,pa+1,kati,make)

ans = []
s = str(input())

dfs(0,0,0,0,0)
print(max(ans))

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)
D = {}
for i in range(N):
    if s[i] in D:
        D[s[i]] += 1
    else:
        D[s[i]] = 1
my_p = N//2
score = my_p - D["p"]
print(score)

Submission status: Runtime Error

Solution code:
s = input()
N = len(s)
D = {}
for i in range(N):
    if s[i] in D:
        D[s[i]] += 1
    else:
        D[s[i]] = 1
my_g = (N+1)//2
my_p = N//2
score = my_p - D["p"]
print(score)

Submission status: Runtime Error

Solution code:
s=input()
n=len(s)
t=0
for i in range(n):
  if s[i]==p:
    t+=1
print(int(n/2)-t)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const long double PI = (acos(-1));
#define rep(i, x, n) for (int i = x; i < (int)(n); i++)
#define sc(x) scanf("%d",&x)
#define scll(x) scanf("%lld",&x)
int main(){
    string s; cin >> s;
    int n = s.size(), p = 0, g = 0, ans = 0;
    rep(i, 0, n){
        if (s[i]=='p'){
            if (p < g) p++;
            else ans--, g++;
        }else {
            if (p < g) ans++, p++;
            else g++;
        }
    }
    cout << ans << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
print(input().count('g')-(-((-len(s))//2)))

Submission status: Runtime Error

Solution code:
s = input()

dp = {i:dict() for i in range(len(s))}
def f(i, g):
    if i == len(s):
        return 0
    if g in dp[i]:
        return dp[i][g]
    p = i - g
    ans = (-1 if s[i] == 'p' else 0) + f(i + 1, g + 1)
    if p + 1 <= g:
        ans = max(ans, (1 if s[i] == 'g' else 0) + f(i + 1, g))
    dp[i][g] = ans
    return ans


print(f(0, 0))

Submission status: Runtime Error

Solution code:
s = input()
g = 0
p = 0
for i in s:
    if i = "g":
        g += 1
    if i = "p":
        p += 1
print((g - p) // 2)

Submission status: Runtime Error

Solution code:
# dpぽい気がするのでそうする
# なぜかREなる....
# pythonだとrecursion limitがアレなのを忘れていた...
# なぜかTLEなる....

import sys

sys.setrecursionlimit(10 ** 8)


s = input()
dp = {}


def rec(i, cur_g, cur_p, tmp, g, p):
    if i in dp:
        return dp[i]
    if i == len(s):
        ret = p - g
        dp[i] = ret
        return ret
    if cur_g > cur_p:
        # パーを出せる（出さなくてもいい）
        if s[i] == "g":
            ret = max(
                rec(i + 1, cur_g, cur_p + 1, tmp + "p", g, p + 1),
                rec(i + 1, cur_g + 1, cur_p, tmp + "g", g, p),
            )
            dp[i] = ret
            return ret
        elif s[i] == "p":
            ret = max(
                rec(i + 1, cur_g, cur_p + 1, tmp + "p", g, p),
                rec(i + 1, cur_g + 1, cur_p, tmp + "g", g + 1, p),
            )
            dp[i] = ret
            return ret
    else:
        # 勝てるかどうかにかかわらずグーを出すしか無い
        if s[i] == "g":
            ret = rec(i + 1, cur_g + 1, cur_p, tmp + "g", g, p)
            dp[i] = ret
            return ret
        elif s[i] == "p":
            ret = rec(i + 1, cur_g + 1, cur_p, tmp + "g", g + 1, p)
            dp[i] = ret
            return ret


print(rec(0, 0, 0, "", 0, 0))

Submission status: Runtime Error

Solution code:
# dpぽい気がするのでそうする
# なぜかREなる....

s = input()
dp = {}


def rec(i, cur_g, cur_p, tmp):
    # if i in dp:
    #     return dp[i]
    if i == len(s):
        ret = 0
        for j in range(len(s)):
            if tmp[j] == "p" and s[j] == "g":
                ret += 1
            elif tmp[j] == "g" and s[j] == "p":
                ret -= 1
        return ret
    if cur_g > cur_p:
        # パーを出せる（出さなくてもいい）
        ret = max(
            rec(i + 1, cur_g, cur_p + 1, tmp + "p"),
            rec(i + 1, cur_g + 1, cur_p, tmp + "g"),
        )
        # dp[i] = ret
        return ret

    else:
        # 勝てるかどうかにかかわらずグーを出すしか無い
        ret = rec(i + 1, cur_g + 1, cur_p, tmp + "g")
        # dp[i] = ret
        return ret


print(rec(0, 0, 0, ""))

Submission status: Runtime Error

Solution code:
# dpぽい気がするのでそうする
# なぜかREなる....

s = input()
dp = {}


def rec(i, cur_g, cur_p, tmp):
    if i in dp:
        return dp[i]
    if i == len(s):
        ret = 0
        for j in range(len(s)):
            if tmp[j] == "p" and s[j] == "g":
                ret += 1
            elif tmp[j] == "g" and s[j] == "p":
                ret -= 1
        return ret
    if cur_g > cur_p:
        # パーを出せる（出さなくてもいい）
        ret = max(
            rec(i + 1, cur_g, cur_p + 1, tmp + "p"),
            rec(i + 1, cur_g + 1, cur_p, tmp + "g"),
        )
        dp[i] = ret
        return ret

    else:
        # 勝てるかどうかにかかわらずグーを出すしか無い
        ret = rec(i + 1, cur_g + 1, cur_p, tmp + "g")
        dp[i] = ret
        return ret


print(rec(0, 0, 0, ""))

Submission status: Runtime Error

Solution code:
# dpぽい気がするのでそうする
s = input()
dp = {}


def rec(i, cur_g, cur_p, tmp):
    if i in dp:
        return dp[i]
    if i == len(s):
        ret = 0
        for j in range(len(s)):
            if tmp[j] == "p" and s[j] == "g":
                ret += 1
            elif tmp[j] == "g" and s[j] == "p":
                ret -= 1
        return ret
    if cur_g > cur_p:
        # パーを出せる（出さなくてもいい）
        if s[i] == "g":
            ret = max(
                rec(i + 1, cur_g, cur_p + 1, tmp + "p"),
                rec(i + 1, cur_g + 1, cur_p, tmp + "g"),
            )
            dp[i] = ret
            return ret
        else:
            ret = max(
                rec(i + 1, cur_g, cur_p + 1, tmp + "p"),
                rec(i + 1, cur_g + 1, cur_p, tmp + "g"),
            )
            dp[i] = ret
            return ret

    else:
        # 勝てるかどうかにかかわらずグーを出すしか無い
        ret = rec(i + 1, cur_g + 1, cur_p, tmp + "g")
        dp[i] = ret
        return ret


print(rec(0, 0, 0, ""))

Submission status: Runtime Error

Solution code:
s = list(input())
n = len(s)
ans = 0
for i in range(n):
  if s[i] == "g" and flag == 0:
    flag+=1
  elif s[i] == "g" and flag != 0:
    flag -= 1
    ans += 1
  elif s[i] == "p" and flag == 0:
    flag += 1
    ans -= 1
  else:
    flag -= 1
print(ans)

Submission status: Runtime Error

Solution code:
#D-atcoderくんと変なジャンケン
s=input()
import collections
S=dict(collections.Counter(s))
a=S["p"]
b=S["g"]
x=((a+b)//2)
print(x-a)

Submission status: Runtime Error

Solution code:
import numpy as np

s = input()
N = len(s)

# 縦：じゃんけんの回数(0回含む）、横：パーを出した回数 そのときの最大スコア
# 1回目のじゃんけんではパーは出せない
scores = np.zeros((N + 1, N + 1), np.int64)

for i in range(2, N + 1):
    if s[i - 1] == 'g':
        scores[i, 0] = scores[i - 1, 0]
        scores[i, 1:i // 2 + 1] = np.maximum(scores[i - 1, :i // 2] + 1, scores[i - 1, 1:i // 2 + 1])
    else:
        scores[i, 0] = scores[i - 1, 0] - 1
        scores[i, 1:i // 2 + 1] = np.maximum(scores[i - 1, :i // 2], scores[i - 1, 1:i // 2 + 1] - 1)

print(scores[-1].max())

Submission status: Runtime Error

Solution code:
import numpy as np

s = input()
N = len(s)

# 縦：じゃんけんの回数(0回含む）、横：パーを出した回数 そのときの最大スコア
# 1回目のじゃんけんではパーは出せない
scores = np.zeros((N + 1, N // 2 + 1), np.int64)

for i in range(2, N + 1):
    if s[i - 1] == 'g':
        scores[i, 0] = scores[i - 1, 0]
        scores[i, 1:i // 2 + 1] = np.maximum(scores[i - 1, :i // 2] + 1, scores[i - 1, 1:i // 2 + 1])
    else:
        scores[i, 0] = scores[i - 1, 0] - 1
        scores[i, 1:i // 2 + 1] = np.maximum(scores[i - 1, :i // 2], scores[i - 1, 1:i // 2 + 1] - 1)

print(scores[-1].max())

Submission status: Runtime Error

Solution code:
import numpy as np

s = input()
N = len(s)

# 縦：じゃんけんの回数、横：パーを出した回数 そのときの最大スコア
# 1回目のじゃんけんではパーは出せない
scores = np.zeros((N, N // 2 + 1), np.int64)

for i in range(1, N):
    if s[i] == 'g':
        scores[i, 0] = scores[i - 1, 0]
        scores[i, 1:i // 2 + 2] = np.maximum(scores[i - 1, :i // 2 + 1] + 1, scores[i - 1, 1:i // 2 + 2])
    else:
        scores[i, 0] = scores[i - 1, 0] - 1
        scores[i, 1:i // 2 + 2] = np.maximum(scores[i - 1, :i // 2 + 1], scores[i - 1, 1:i // 2 + 2] - 1)

print(scores[-1].max())

Submission status: Runtime Error

Solution code:
tcd = input()

def dp(i, cnt, g, p):
    if g == p:
        if tcd[i] == "p":
            cnt -= 1
        g += 1

    else:
        if tcd[i] == "g":
            cnt += 1
        p += 1
    if i == len(tcd)-1:
        return cnt
    else:
        return dp(i+1, cnt, g, p)

print(dp(1, 0, 1, 0))

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
// #define pair pair<ll,ll> 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>


const ll mod =1e9+7;




int main(){

    string s;
    cin>>s;
    ll g=0,p=0;
    ll ans=0;
    for(ll i=0;s[i];i++){
        if(g>p){
            p++;
            ans+=(s[i]=='g');
        }
        else{
            g++;
            ans-=(s[i]=='p');
        }
    }
    cout<<ans<<endl;
}

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import copy
S=list(input())
N=len(S)
point=0
hand=copy.deepcopy(S)
panum=sum([j=="p" for j in hand])
pamax=N//2#ポイントの上限
for i in range(N):
    if S[i]=="g":
        pa=sum([hand[j]=="p" for j in range(i+1)])#今まで出したパーの数
        if pa<(i+1)//2 and point<pamax-panum:
            hand[i]="p"
            point+=1
    if point=>pamax-panum:
        break
print(point)

Submission status: Runtime Error

Solution code:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int len = s.size();
    //nump <= numg
    int nump = 0, numg = 0;
    for(int i = 0 ; i < len; i++){
        if(s[i] == 'p')nump++;
        else if(s[i] == 'g')numg++;
    }
    cout<<(nump+numg)/2-nump;
    return 0;
}