Buggy python submission (verdict: Time Limit Exceeded):
#-*-coding:utf-8-*-

def count(s, c):
    cnt = 0
    for i in s:
        if i == c:
            cnt += 1
    return cnt

def main():
    #入力
    N = int(input())
    S = input()

    min_cnt = 300000
    ew = ['E', 'W']

    for i in range(N):
        first_str = S[:i]
        last_str = S[i+1:]

        cnt = 0
        cnt_too_much_flag = 0

        for j in first_str:
            if j == 'W':
                cnt += 1
            elif cnt_too_much_flag == 1:
                break
            elif cnt > min_cnt:
                cnt_too_much_flag = 1
        for j in last_str:
            if j == 'E':
                cnt += 1
            elif cnt_too_much_flag == 1:
                break
            elif cnt > min_cnt:
                cnt_too_much_flag = 1

        if cnt < min_cnt:
            min_cnt = cnt
        #print(first_str, last_str, cnt)
    
    
    print(min_cnt)
            

        


if __name__ == '__main__':
    main()

Fixed submission:
#-*-coding:utf-8-*-

#累積和を求める
def get_cum_sum(s, c):
	cum_sum =[]
	cnt = 0
	
	for i in s:
		if i == c:
			cnt += 1
			cum_sum.append(cnt)
		else:
			cum_sum.append(cnt)
			
	return cum_sum
    
	

def main():
    #入力
    N = int(input())
    S = input()
		
    cum_sum_w = get_cum_sum(S, 'W')
    cum_sum_e = get_cum_sum(S, 'E')
    min_cnt = 300000
    
    for i in range(N):
    	if S[i] == 'W':
    		first_total_w = cum_sum_w[i] - 1
    	else:
    		first_total_w = cum_sum_w[i]
    		
    	last_total_e = cum_sum_e[-1] - cum_sum_e[i]
    	
    	#print(S[:i], S[i+1:], first_total_w, last_total_e)
    	
    	if min_cnt > first_total_w + last_total_e:
    		min_cnt = first_total_w + last_total_e
    
    print(min_cnt)
    	
    	
        


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#-*-coding:utf-8-*-

def count(s, c):
    cnt = 0
    for i in s:
        if i == c:
            cnt += 1
    return cnt

def main():
    #入力
    N = int(input())
    S = input()

    min_cnt = 300000
    ew = ['E', 'W']

    for i in range(N):
        first_str = S[:i]
        last_str = S[i+1:]

        cnt = 0
        cnt += count(first_str, 'W')
        cnt += count(last_str, 'E')
        if cnt < min_cnt:
            min_cnt = cnt
        #print(first_str, last_str, cnt)
    
    
    print(min_cnt)
            

        


if __name__ == '__main__':
    main()

Fixed submission:
#-*-coding:utf-8-*-

#累積和を求める
def get_cum_sum(s, c):
	cum_sum =[]
	cnt = 0
	
	for i in s:
		if i == c:
			cnt += 1
			cum_sum.append(cnt)
		else:
			cum_sum.append(cnt)
			
	return cum_sum
    
	

def main():
    #入力
    N = int(input())
    S = input()
		
    cum_sum_w = get_cum_sum(S, 'W')
    cum_sum_e = get_cum_sum(S, 'E')
    min_cnt = 300000
    
    for i in range(N):
    	if S[i] == 'W':
    		first_total_w = cum_sum_w[i] - 1
    	else:
    		first_total_w = cum_sum_w[i]
    		
    	last_total_e = cum_sum_e[-1] - cum_sum_e[i]
    	
    	#print(S[:i], S[i+1:], first_total_w, last_total_e)
    	
    	if min_cnt > first_total_w + last_total_e:
    		min_cnt = first_total_w + last_total_e
    
    print(min_cnt)
    	
    	
        


if __name__ == '__main__':
    main()