39A - C++ Calculations - CodeForces Solution


expression parsing greedy *2000

Please click on ads to support us..

Python Code:

from typing import List, Tuple, Optional
from collections import deque, Counter
from queue import PriorityQueue
import math
from functools import lru_cache
import random
import copy
import sys
sys.setrecursionlimit(9999)


MOD = 10**9 + 7



init_a = int(input())
s = input()

n = len(s)
i = 0

ss = ""
while i < n:
    if i+2 <= n-1 and (s[i:i+3] == 'a++' or s[i:i+3] == '++a'):
        ss += "X" if s[i:i+3] == 'a++' else 'Y'
        i += 3
    else:
        ss += s[i]
        i += 1

if ss[0] != '-':
    ss = '+' + ss

arr = []
f = []
n = len(ss)
i = 0
while i < n:
    j = i
    while j+1 < n and (ss[j+1] != 'X' and ss[j+1] != 'Y'):
        j += 1

    if ss[j] == '*':
        v = int(ss[i: j])
        arr.append((v, ss[j+1]))
    else:
        if ss[i] == '-':
            arr.append((-1, ss[j+1]))
        else:
            arr.append((1, ss[j+1]))

    i = j+2


arr.sort()

a = init_a
tot = 0
for v, ch in arr:
    if ch == 'Y':
        a += 1

    tot += a * v

    if ch == 'X':
        a += 1


print(tot)


Comments

Submit
0 Comments
More Questions

1735B - Tea with Tangerines
1735C - Phase Shift
1321C - Remove Adjacent
281B - Nearest Fraction
1043A - Elections
1598C - Delete Two Elements
1400C - Binary String Reconstruction
1734D - Slime Escape
1499A - Domino on Windowsill
991A - If at first you don't succeed
1196C - Robot Breakout
373A - Collecting Beats is Fun
965A - Paper Airplanes
863E - Turn Off The TV
630E - A rectangle
1104A - Splitting into digits
19C - Deletion of Repeats
1550B - Maximum Cost Deletion
1693A - Directional Increase
735D - Taxes
989A - A Blend of Springtime
339C - Xenia and Weights
608A - Saitama Destroys Hotel
1342C - Yet Another Counting Problem
548A - Mike and Fax
109A - Lucky Sum of Digits
864C - Bus
626B - Cards
1221A - 2048 Game
1374D - Zero Remainder Array