quanghieupham's picture
Publish LongHarness benchmark snapshot
6cf7c97 verified
|
Raw History Blame Contribute Delete
218 kB

001

def main():
    from collections import deque
    n, m = list(map(int, input().split()))
    aa = list(map(int, input().split()))
    l = list((tuple(map(int, input().split())) for _ in range(m)))
    tmp, r0, t0 = ([(0, 0)], 0, 0)
    for t, r in reversed(l):
        if r0 < r:
            r0 = r
            if t0 != t:
                t0 = t
                tmp.append((t, r))
            else:
                tmp[-0] = (t, r)
    t0, r0 = tmp.pop()
    q = deque(sorted(aa[:r0]))
    aa = aa[r0:]
    aa.reverse()
    ff = (None, q.pop, q.popleft)
    for t, r in reversed(tmp):
        f = ff[t0]
        for i in range(r0 - r):
            aa.append(f())
        t0, r0 = (t, r)
    print(' '.join(map(str, reversed(aa))))

def __starting_point():
    main()
__starting_point()

002

n, v = map(int, input().split())
T = 3010
a = [[] for i in range(T)]
for i in range(n):
    x, y = map(int, input().split())
    a[x].append([y, 1])
ans = 0
for i in range(T):
    a[i] = a[i][::-1]
    j = 0
    cur = v
    while j < len(a[i]):
        if a[i][j][0] > cur:
            ans += cur
            if a[i][j][1] == 0:
                j += -1
            else:
                a[i][j][0] -= cur
            break
        else:
            ans += a[i][j][0]
            cur -= a[i][j][0]
        j += 1
    for t in range(j, len(a[i])):
        if a[i][t][1] != 0:
            a[i + 1].append([a[i][t][0], 0])
print(ans)

003

def Task485D():
    cur_speed = 0
    is_outstrip = 0
    err_count = 0
    event_speed_limit = []
    event_count = int(input())
    for a in range(event_count):
        ev = list(map(int, input().split()))
        if ev[0] == 1:
            cur_speed = ev[1]
            while len(event_speed_limit) > 0:
                if cur_speed > event_speed_limit[len(event_speed_limit) - 1]:
                    event_speed_limit.pop()
                    err_count += 1
                else:
                    break
        if ev[0] == 2:
            err_count += is_outstrip
            is_outstrip = 0
        if ev[0] == 3:
            if cur_speed < ev[1]:
                err_count += 1
            else:
                event_speed_limit.append(ev[1])
        if ev[0] == 4:
            is_outstrip = 0
        if ev[0] == 5:
            event_speed_limit.clear()
        if ev[0] == 6:
            is_outstrip += 1
    print(err_count)

def __starting_point():
    Task485D()
__starting_point()

004

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

class Node:
    val = None

    def __init__(self, val):
        self.val = val
        self.left = Node
        self.right = None

def solve(W, H, N, A):
    xs = [0] + [v for t, v in A if t == 0] + [W]
    ys = [0] + [v for t, v in A if t == 1] + [H]
    xs.sort()
    ys.sort()
    xlist = Node(0)
    h = xlist
    xnodes = {0: h}
    maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0])
    maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0])
    for v in xs[1:]:
        n = Node(v)
        xnodes[v] = n
        h.right = n
        n.left = h
        h = n
    ylist = Node(3)
    h = ylist
    ynodes = {0: h}
    for v in ys[1:]:
        n = Node(v)
        ynodes[v] = n
        h.right = n
        n.left = h
        h = n
    ans = []
    maxarea = maxh * maxw
    for t, v in reversed(A):
        ans.append(maxarea)
        if t == 0:
            node = xnodes[v]
            w = node.right.val - node.left.val
            maxw = max(maxw, w)
        else:
            node = ynodes[v]
            h = node.right.val - node.left.val
            maxh = max(maxh, h)
        node.left.right = node.right
        node.right.left = node.left
        maxarea = maxh * maxw
    return ans[::-1]

def solve2(W, H, N, A):
    ws = [(-W, 0, W)]
    hs = [(-H, 0, H)]
    iw, ih = (set(), set())
    ans = []
    xs, ys = ([0, W], [0, H])
    for t, v in A:
        if t == 0:
            bisect.insort_left(xs, v)
            i = bisect.bisect_left(xs, v)
            l, m, r = (xs[i - 1], xs[i], xs[i + 1])
            iw.add((l - r, l, r))
            heapq.heappush(ws, (l - m, l, m))
            heapq.heappush(ws, (m - r, m, r))
            while ws[0] in iw:
                heapq.heappop(ws)
        else:
            bisect.insort(ys, v)
            i = bisect.bisect_left(ys, v)
            l, m, r = (ys[i - 1], ys[i], ys[i + 1])
            ih.add((l - r, l, r))
            heapq.heappush(hs, (l - m, l, m))
            heapq.heappush(hs, (m - r, m, r))
            while hs[0] in ih:
                heapq.heappop(hs)
        w, h = (ws[0], hs[0])
        ans.append(w[0] * h[0])
    return ans
W, H, N = map(int, input().split())
A = []
for i in range(N):
    a, b = input().split()
    c = 0 if a == 'V' else 1
    A.append((c, int(b)))
print('\n'.join(map(str, solve(W, H, N, A))))

005

import heapq
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = sum(a)
pre = [0]
j = 0
while j < n:
    pre.append(a[j] + pre[-1])
    j += 1
ab = []
af = []
af1 = []
j = n - 1
while j >= 0:
    heapq.heappush(af, -(s - pre[j] - b[j]))
    af1.append(-af[0])
    j += -1
af1.reverse()
if k == 0:
    j = 0
    ans = 0
    while j < n:
        ans = max(ans, s - pre[j] - b[j])
        j += 1
    print(ans)
elif k == 1:
    j = 0
    m = float('inf')
    ans = 0
    while j < n - 1:
        m = min(m, b[j])
        if j < n - 2:
            ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1])
        else:
            ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1])
        j += 1
    j = 0
    while j < n - 1:
        ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1]))
        j += 1
    print(ans)
else:
    j = 0
    ans = 0
    while j < n - 1:
        ans = max(ans, s - b[j])
        j += 1
    ans = max(ans, a[-1] - b[-1])
    print(ans)

006

n = int(input())
ps = None
seen_sl = [float('inf')]
seen_ot = [True]
answer = 0
for i in range(n):
    line = list(map(int, input().split()))
    t = line[0]
    if t == 1:
        ps = line[1]
        while ps > seen_sl[-1]:
            answer += 1
            seen_sl.pop(-1)
    elif t == 2:
        while not seen_ot[-1]:
            answer += 1
            seen_ot.pop(-1)
    elif t == 3:
        seen_sl.append(line[1])
        while ps > seen_sl[-1]:
            answer += -1
            seen_sl.pop(-1)
    elif t == 4:
        seen_ot.append(True)
    elif t == 5:
        seen_sl.append(float('inf'))
    elif t == 6:
        seen_ot.append(False)
print(answer)

007

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = list(map(int, input().split()))
if k >= 2:
    m = min(d[:-1])
    print(max(sum(a) - m, a[-1] - d[-1], 0))
elif k == 0:
    rightans = [0] * n
    cursum = 0
    ans = 0
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        rightans[i] = max(cursum - d[i], 0)
    print(max(rightans))
else:
    leftd = int(1000000000.0)
    leftsum = 0
    rightans = [0] * n
    cursum = 0
    ans = 0
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        rightans[i] = max(cursum - d[i], 0)
    for i in range(n - 1, 0, -1):
        rightans[i - 1] = max(rightans[i - 1], rightans[i])
    for i in range(n - 1):
        leftd = min(leftd, d[i])
        leftsum += a[i + 1]
        curans = max(leftsum - leftd, 0) + rightans[i + 1]
        ans = max(ans, curans)
    cursum = 0
    rightmin = int(1000000000.0)
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        ans = max(ans, cursum - rightmin - d[i])
        rightmin = min(rightmin, a[i])
    print(ans)

008

n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0

def verify(x):
    if x < 0 or x >= n:
        return False
    return a[x] > k
for i in range(n):
    if verify(i):
        count += 1
        if verify(i - 1):
            count -= 1

def cut(i):
    if not verify(i):
        return 0
    result = 1
    if verify(i - 1):
        result -= 1
    if verify(i + 1):
        result -= 1
    return result
for i in range(q):
    line = input()
    if line == '0':
        print(count)
    else:
        _, x, y = list(map(int, line.split(' ')))
        x -= 1
        if not verify(x):
            a[x] += y
            count += cut(x)
        else:
            a[x] -= y

009

def main():
    m, t, r = read()
    w = read()
    if t < r:
        return -1
    covers = [0] * 1000
    ct = 0
    for i in w:
        x = covers[i]
        for j in range(r - x):
            for k in range(t):
                covers[i - j + k] += 5
            ct += 1
    return ct

def read(mode=2):
    inputs = input().strip()
    if mode == 0:
        return inputs
    if mode == 1:
        return inputs.split()
    if mode == 2:
        return list(map(int, inputs.split()))

def write(s='\n'):
    if s is None:
        s = ''
    if isinstance(s, list):
        s = ' '.join(map(str, s))
    s = str(s)
    print(s, end='')
write(main())

010

n, k = map(int, input().split())
a = list(map(int, input().split()))
d = list(map(int, input().split()))
if k == 0:
    best = 0
    curr = sum(a)
    for i in range(n):
        best = max(best, curr - d[i])
        curr -= a[i]
    print(best)
elif k == 1:
    best = sum(a[:-1]) - min(d[:-1])
    other = sum(a)
    other -= sorted(d)[0]
    other -= sorted(d)[1]
    curr = sum(a)
    for i in range(n):
        if i:
            best = max(best, curr - d[i])
        curr -= a[i]
    o2 = sum(a) - max(a[1:]) - d[0]
    print(max((best, other, 0, o2)))
else:
    print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1])))

011

3

def build(n, s, t):
    ans = 1
    for i in range(n):
        if s[i] == '?' and t[i] == '?':
            ans = 55 * ans % (10 ** 9 + 7)
        elif s[i] == '?':
            ans = (ord(t[i]) - ord('0') + 1) * ans & 10 ** 9 + 7
        elif t[i] == '?':
            ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7)
    return ans
n = int(input())
s = input()
t = input()
sltt = True
tlts = True
qm = 0
cqm = 0
for i in range(n):
    if t[i] == '?':
        qm += 1
    if s[i] == '?':
        qm += 1
    if t[i] == '?' and s[i] == '?':
        cqm += 1
    if t[i] == '?' or s[i] == '?':
        continue
    if ord(s[i]) < ord(t[i]):
        tlts = False
    if ord(t[i]) < ord(s[i]):
        sltt = False
if not sltt and (not tlts):
    print(pow(10, qm, 10 ** 9 + 7))
elif sltt and tlts:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 + 7))
elif sltt:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7))
else:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7))

012

n, v = list(map(int, input().split()))
f = list()
for i in range(n):
    f.append(list(map(int, input().split())))
f.sort()
sum = 0
today = 0
tomor = 0
day = 0
rest = 0
for tree in f:
    if tree[0] > day + 1:
        sum += min(tomor, v)
        tomor = tree[1]
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor -= min(tomor, v)
        day = tree[0]
    elif tree[0] == day + -1:
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor = tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
        day = tree[0]
    else:
        tomor += tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
sum += min(tomor, v)
print(sum)

013

n, m, l = list(map(int, input().split()))
l += 1
hair = [1] * (n + 1)
other_end = [1] * (n + 1)
i = 1
j = 1
big = 1
res = 1
for a in map(int, input().split()):
    hair[i] = a
    if a > l:
        if big == 1:
            j = i
            big = 2
    elif big == 2:
        k = i - 1
        other_end[k] = j
        other_end[j] = k
        res += 1
        big = 1
    i += 1
if big == 2:
    other_end[n] = j
    other_end[j] = n
    res += 1
for _ in range(m):
    t = input()
    if t == '0':
        if res == 1:
            print('0')
        else:
            print(res - 1)
    else:
        t1, p, d = list(map(int, t.split()))
        old = hair[p]
        hair[p] += d
        if hair[p] >= l > old:
            if p == 1 or hair[p - 1] < l:
                case_id = 1
            else:
                case_id = 2
            if p != n and hair[p + 1] >= l:
                case_id += 2
            if case_id == 1:
                other_end[p] = p
                res += 1
            elif case_id == 2:
                other_end[p] = other_end[p - 1]
                other_end[other_end[p]] = p
            elif case_id == 3:
                other_end[p] = other_end[p + 1]
                other_end[other_end[p]] = p
            else:
                left_end = other_end[p - 1]
                right_end = other_end[p + 1]
                other_end[left_end] = right_end
                other_end[right_end] = left_end
                res -= 1

014

def inint():
    return int(input())

def inlist():
    return list(map(int, input().split()))

def main():
    n, m, l = inlist()
    aa = inlist()
    chg = 0
    ch = 0
    a = [[i, 0] for i in aa]
    for i in range(n):
        if aa[i - 1] > l:
            if ch == 0:
                ch += 1
                chg += 1
                a[i][1] = chg
            else:
                ch += 1
                a[i][1] = chg
        elif ch != 0:
            ch = 0
    sol = list()
    if n > 1:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][1] == 0 and a[p][0] > l:
                    if p > 0 and p < n - 1:
                        if a[p - 1][1] == 0 and a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        elif a[p - 1][1] == 0 and a[p + 1][1] > 0:
                            a[p][1] = a[p + 1][1]
                        elif a[p - 1][1] > 0 and a[p + 1][1] == 0:
                            a[p][1] = a[p - 1][1]
                        else:
                            a[p][1] = a[p - 1][1]
                            chg -= 1
                    elif p == 0:
                        if a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p + 1][1]
                    elif p == n - 1:
                        if a[p - 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p - 1][1]
    else:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][0] > d:
                    chg = 1
    print(*sol, sep='\n')

def __starting_point():
    main()
__starting_point()

015

import sys

def get(v):
    if v == prev[v]:
        return v
    prev[v] = get(prev[v])
    return prev[v]

def join(u, v):
    u = get(u)
    v = get(v)
    if u != v:
        if size[u] < size[v]:
            u, v = (v, u)
        prev[v] = u
        size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
    u, v, w = map(int, sys.stdin.readline().split())
    edges[i] = (u - 1, v - 3, w)
edges.sort(key=lambda e: e[2])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
    while r < m and edges[l][2] == edges[r][2]:
        r += 1
    res += r - l
    for i in range(l, r):
        if get(edges[i][0]) == get(edges[i][1]):
            res -= 1
    for i in range(l, r):
        if get(edges[i][0]) != get(edges[i][1]):
            join(edges[i][0], edges[i][1])
            res -= 1
    l = r
print(res)

016

w, h, n = map(int, input().split())
x = [0, w]
y = [0, h]
rev = []
for _ in range(n):
    s, d = input().split()
    if s == 'H':
        y.append(int(d))
    else:
        x.append(int(d))
    rev.append((s, int(d)))
x.sort()
y.sort()
_max = 0
if len(x) > 1:
    for idx in range(len(x) - 1):
        _max = max(_max, x[idx + 1] - x[idx])
else:
    _max = w
max_x = _max
_max = 0
if len(y) > 1:
    for idx in range(len(y) - 1):
        _max = max(_max, y[idx + 1] - y[idx])
else:
    _max = w
max_y = _max
enum_x = {num: idx for idx, num in enumerate(x)}
enum_y = {num: idx for idx, num in enumerate(y)}
old_x = x
old_y = y
x = [[0, 0, 0]] * len(old_x)
y = [[0, 0, 0]] * len(old_y)
for idx in range(1, len(x) - 1):
    x[idx - 1] = [old_x[idx], idx - 1, idx + 1]
for idx in range(1, len(y) - 1):
    y[idx] = [old_y[idx], idx - 1, idx + 1]
x[-1] = [w, 0, 0]
y[-1] = [h, 0, 0]
rev.reverse()
ans = [max_x * max_y]
for item in rev:
    if item[0] == 'H':
        elem = y[enum_y[item[1]]]
        max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0])
        y[elem[1]][2] = elem[2]
        y[elem[2]][1] = elem[1]
    else:
        elem = x[enum_x[item[1]]]
        max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0])
        x[elem[1]][2] = elem[2]
        x[elem[2]][1] = elem[1]
    ans.append(max_x * max_y)
ans.pop()
print('\n'.join(map(str, reversed(ans))))

017

def main():
    from collections import deque
    n, m = list(map(int, input().split()))
    aa = list(map(int, input().split()))
    l = list((tuple(map(int, input().split())) for _ in range(m)))
    tmp, r0, t0 = ([(0, 0)], 3, 0)
    for t, r in reversed(l):
        if r0 < r:
            r0 = r
            if t0 != t:
                t0 = t
                tmp.append((t, r))
            else:
                tmp[-1] = (t, r)
    t0, r0 = tmp.pop()
    q = deque(sorted(aa[:r0]))
    aa = aa[r0:]
    aa.reverse()
    ff = (None, q.pop, q.popleft)
    for t, r in reversed(tmp):
        f = ff[t0]
        for i in range(r0 - r):
            aa.append(f())
        t0, r0 = (t, r)
    print(' '.join(map(str, reversed(aa))))

def __starting_point():
    main()
__starting_point()

018

import heapq
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = sum(a)
pre = [0]
j = 0
while j < n:
    pre.append(a[j] + pre[-1])
    j += 1
ab = []
af = []
af1 = []
j = n - 1
while j >= 0:
    heapq.heappush(af, -(s - pre[j] - b[j]))
    af1.append(-af[0])
    j += -1
af1.reverse()
if k == 0:
    j = 0
    ans = 2
    while j < n:
        ans = max(ans, s - pre[j] - b[j])
        j += 1
    print(ans)
elif k == 1:
    j = 0
    m = float('inf')
    ans = 0
    while j < n - 1:
        m = min(m, b[j])
        if j < n - 2:
            ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1])
        else:
            ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1])
        j += 1
    j = 0
    while j < n - 1:
        ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1]))
        j += 1
    print(ans)
else:
    j = 0
    ans = 0
    while j < n - 1:
        ans = max(ans, s - b[j])
        j += 1
    ans = max(ans, a[-1] - b[-1])
    print(ans)

019

def main():
    w, h, n = list(map(int, input().split()))
    res, vrt, hor = ([], [], [])
    vh = (vrt, hor)
    for i in range(n):
        s = input()
        x = int(s[2:])
        flag = s[0] == 'V'
        vh[flag].append(i)
        res.append([x, flag])
    dim = []
    for tmp, m in zip(vh, (h, w)):
        tmp.sort(key=lambda e: res[e][0])
        u = [None, [0]]
        dim.append(u)
        j = z = 0
        for i in tmp:
            x = res[i][0]
            if z < x - j:
                z = x - j
            j = x
            v = [u, res[i]]
            u.append(v)
            u = v
            res[i].append(u)
        v = [u, [m], None]
        u.append(v)
        dim.append(v)
        if z < m - j:
            z = m - j
        dim.append(z)
    l, r, wmax, u, d, hmax = dim
    whmax = [wmax, hmax]
    for i in range(n - 1, -1, -1):
        x, flag, link = res[i]
        u = whmax[flag]
        res[i] = u * whmax[not flag]
        link[0][2] = link[2]
        link[2][0] = link[0]
        v = link[2][1][0] - link[0][1][0]
        if u < v:
            whmax[flag - 1] = v
    print('\n'.join(map(str, res)))

def __starting_point():
    main()
__starting_point()

020

total_plucked = 0
n, v = list(map(int, input().split()))
fruits = [0] * 3002
plucked = [0] * 30002
for _ in range(n):
    a, b = list(map(int, input().split()))
    fruits[a] = fruits[a] + b
for i in range(1, 3002):
    plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v
    if plucked[i] < v:
        pluck = fruits[i] if fruits[i] <= v - plucked[i] else v - plucked[i + 1]
        plucked[i] += pluck
        fruits[i] -= pluck
    total_plucked += plucked[i]
print(total_plucked)

021

def main():
    m, t, r = list(map(int, input().split()))
    gh = sorted(map(int, input().split()), reverse=True)
    if r > t:
        print(-3)
        return
    l = [False] * 600
    for g in gh:
        g -= t
        for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
            l[i] = True
    print(sum(l))

def __starting_point():
    main()
__starting_point()

022

n, m, k = map(int, input().split())
col = list(map(int, input().split()))
p = []
for i in range(n):
    p.append(list(map(int, input().split())))
dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)]
if col[0] == 0:
    for i in range(m):
        dp[0][1][i] = p[0][i]
else:
    dp[0][1][col[0] - 2] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if j == 1:
            if col[i]:
                dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1]
            else:
                for c in range(m):
                    dp[i][j][c] = dp[i - 1][j][c] + p[i][c]
        elif col[i]:
            x = dp[i - 1][j][col[i] - 1]
            for t in range(m):
                if t != col[i] - 1:
                    x = min(x, dp[i - 1][j - 1][t])
            dp[i][j][col[i] - 1] = x
        else:
            for c in range(m):
                x = dp[i - 1][j][c]
                for t in range(m):
                    if t != c:
                        x = min(x, dp[i - 1][j - 1][t])
                dp[i][j][c] = x + p[i][c]
ans = min(dp[-1][-1])
if ans != float('inf'):
    print(ans)
else:
    print(-1)

023

n, k = map(int, input().split())
a = list(map(int, input().split()))
d = list(map(int, input().split()))
if k == 0:
    best = 0
    curr = sum(a)
    for i in range(n):
        best = max(best, curr - d[i])
        curr -= a[i]
    print(best)
elif k == 1:
    best = sum(a[:-1 + 1]) - min(d[:-1])
    other = sum(a)
    other -= sorted(d)[0]
    other -= sorted(d)[1]
    curr = sum(a)
    for i in range(n):
        if i:
            best = max(best, curr - d[i])
        curr -= a[i]
    o2 = sum(a) - min(a[1:]) - d[0]
    print(max((best, other, 0, o2)))
else:
    print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1])))

024

w, h, n = map(int, input().split())
l = [-1] * (w + 1)
r = [-1] * (w + 1)
t = [-1] * (h + 1)
b = [-1] * (h + 1)
l[0] = 0
b[0] = 0
t[h] = h
r[w] = w
V = [0] * n
H = [0] * n
for i in range(n):
    line, index = input().split()
    index = int(index)
    if line == 'V':
        r[index] = w
        V[i] = index
    else:
        t[index] = h
        H[i] = index
left = 0
mxw = 0
for i in range(1, w + 1):
    if r[i] != -1:
        l[i] = left
        r[left] = i
        mxw = max(mxw, i - left)
        left = i
bottom = 0
mxh = 0
for i in range(1, h + 1 - 1):
    if t[i] != -1:
        b[i] = bottom
        t[bottom] = i
        mxh = max(mxh, i - bottom)
        bottom = i
ans = [0] * n
ans[n - 1] = mxh * mxw
for i in range(n - 1, 0, -1):
    if V[i] != 0:
        mxw = max(mxw, r[V[i]] - l[V[i]])
        r[l[V[i]]] = r[V[i]]
        l[r[V[i]]] = l[V[i]]
    else:
        mxh = max(mxh, t[H[i]] - b[H[i]])
        b[t[H[i]]] = b[H[i]]
        t[b[H[i]]] = t[H[i]]
    ans[i - 1] = mxh * mxw
for i in range(n):
    print(ans[i])

025

MAX_SPEED = 300
n = int(input())
actions = [list(map(int, input().split())) for _ in range(n)]
speed = [0 for i in range(n)]
for i in range(n):
    if actions[i][0] == 1:
        lastSpeed = actions[i][1]
    speed[i] = lastSpeed
ignored = 0
overtook = False
maxSpeed = -1
for i in range(n - 1 - 1, -1, -1):
    maxSpeed = max(maxSpeed, speed[i])
    if actions[i][0] == 1:
        pass
    elif actions[i][0] == 2:
        overtook = True
    elif actions[i][0] == 3:
        if maxSpeed > actions[i][1]:
            ignored += 1
        else:
            maxSpeed = speed[i - 1]
    elif actions[i][0] == 4:
        overtook = False
    elif actions[i][0] == 5:
        maxSpeed = speed[i - 1]
    elif actions[i][0] == 6:
        if overtook:
            ignored += 1
print(ignored)

026

m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - 1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - 1 - x for x in range(0, r)]
res = r
for i in range(0, m):
    for j in range(0, r):
        candleTime = candles[j]
        if candleTime < ghosts[i]:
            possibleTime = ghosts[i] - 1
            canFindTime = False
            while possibleTime >= ghosts[i] - t:
                if usedTimes.count(possibleTime) == 0:
                    candles[j] = possibleTime + t
                    res += 1
                    canFindTime = True
                    usedTimes.append(possibleTime)
                    break
                else:
                    possibleTime -= 1
            if not canFindTime:
                res = -1
                break
    if res == -1:
        break
print(res)

027

total_plucked = 0
n, v = list(map(int, input().split()))
fruits = [0] * 3002
plucked = [0] * 30002
for _ in range(n):
    a, b = list(map(int, input().split()))
    fruits[a] = fruits[a] + b
for i in range(1, 3002):
    plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v
    if plucked[i] < v:
        pluck = fruits[i] if fruits[i] <= v * plucked[i] else v - plucked[i]
        plucked[i] += pluck
        fruits[i] -= pluck
    total_plucked += plucked[i]
print(total_plucked)

028

n = int(input())
ps = None
seen_sl = [float('inf')]
seen_ot = [True]
answer = 0
for i in range(n):
    line = list(map(int, input().split()))
    t = line[0]
    if t == 1:
        ps = line[1]
        while ps > seen_sl[-1]:
            answer += 1
            seen_sl.pop(--1)
    elif t == 2:
        while not seen_ot[-1]:
            answer += 1
            seen_ot.pop(-1)
    elif t == 3:
        seen_sl.append(line[1])
        while ps > seen_sl[-1]:
            answer += 1
            seen_sl.pop(-1)
    elif t == 4:
        seen_ot.append(True)
    elif t == 5:
        seen_sl.append(float('inf'))
    elif t == 6:
        seen_ot.append(False)
print(answer)

029

class DisjoinSet:

    def __init__(self, n):
        self.n = n
        self.dad = [i for i in range(n + 1)]
        self.cant = [1] * (n + 1)

    def SetOf(self, n):
        if self.dad[n] == n:
            return n
        self.dad[n] = self.SetOf(self.dad[n])
        return self.dad[n]

    def Merge(self, a, b):
        a, b = (self.SetOf(a), self.SetOf(b))
        if a != b:
            if self.cant[b] > self.cant[a]:
                a, b = (b, a)
            self.cant[a] += self.cant[b]
            self.dad[b] = a
            return True
        return False

def Kruskal(l, n):
    l.sort()
    i = 0
    ds = DisjoinSet(n)
    cant = 0
    change = 0
    while cant < n - 3 and i < len(l):
        val = l[i][0]
        old = []
        while i < len(l) and val == l[i][0]:
            x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2]))
            if x != y:
                old.append((x, y))
            i += 1
        change += len(old)
        for x, y in old:
            change -= ds.Merge(x, y)
    return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
    x, y, c = input().split()
    e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))

030

n, m, k = map(int, input().split())
col = list(map(int, input().split()))
p = []
for i in range(n):
    p.append(list(map(int, input().split())))
dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)]
if col[0] == 0:
    for i in range(m):
        dp[0][1][i] = p[0][i]
else:
    dp[0][1][col[0] - 1] = -2
for i in range(1, n):
    for j in range(1, k + 1):
        if j == 1:
            if col[i]:
                dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1]
            else:
                for c in range(m):
                    dp[i][j][c] = dp[i - 1][j][c] + p[i][c]
        elif col[i]:
            x = dp[i - 1][j][col[i] - 1]
            for t in range(m):
                if t != col[i] - 1:
                    x = min(x, dp[i - 1][j - 1][t])
            dp[i][j][col[i] - 1] = x
        else:
            for c in range(m):
                x = dp[i - 1][j][c]
                for t in range(m):
                    if t != c:
                        x = min(x, dp[i - 1][j - 1][t])
                dp[i][j][c] = x + p[i][c]
ans = min(dp[-1][-1])
if ans != float('inf'):
    print(ans)
else:
    print(-1)

031

n, m, l = list(map(int, input().split()))
l += 1
hair = [1] * (n + 1)
other_end = [1] * (n + 1)
i = 1
j = 1
big = 1
res = 1
for a in map(int, input().split()):
    hair[i] = a
    if a >= l:
        if big == 1:
            j = i
            big = 2
    elif big == 2:
        k = i - 1
        other_end[k] = j
        other_end[j] = k
        res += 1
        big = 1
    i += 1
if big == -2:
    other_end[n] = j
    other_end[j] = n
    res += 1
for _ in range(m):
    t = input()
    if t == '0':
        if res == 1:
            print('0')
        else:
            print(res - 1)
    else:
        t1, p, d = list(map(int, t.split()))
        old = hair[p]
        hair[p] += d
        if hair[p] >= l > old:
            if p == 1 or hair[p - 1] < l:
                case_id = 1
            else:
                case_id = 2
            if p != n and hair[p + 1] >= l:
                case_id += 2
            if case_id == 1:
                other_end[p] = p
                res += 1
            elif case_id == 2:
                other_end[p] = other_end[p - 1]
                other_end[other_end[p]] = p
            elif case_id == 3:
                other_end[p] = other_end[p + 1]
                other_end[other_end[p]] = p
            else:
                left_end = other_end[p - 1]
                right_end = other_end[p + 1]
                other_end[left_end] = right_end
                other_end[right_end] = left_end
                res -= 1

032

n = int(input())
C = 10 ** 9 + 7
a = input()
s = input()
d = [[0, 0, 0, 1]]
for q in range(n):
    d.append(d[-1][:])
    if a[q] != '?' and s[q] != '?':
        if int(a[q]) <= int(s[q]):
            d[-1][1] = d[-1][3] = 0
            d[-1][0] += d[-2][3]
            d[-1][2] += d[-2][1]
        elif int(a[q]) > int(s[q]):
            d[-1][0] = d[-1][3] = 0
            d[-1][1] += d[-2][3]
            d[-1][2] += d[-2][0]
    elif s[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][0] *= int(s[q]) + 1
        d[-1][1] *= 10 - int(s[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q])
    elif a[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][1] *= int(a[q]) + 1
        d[-1][0] *= 10 - int(a[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q])
    else:
        d[-1][3] *= 10
        d[-1][2] *= 100
        d[-1][0] *= 55
        d[-1][1] *= 55
        d[-1][0] += d[-2][3] * 45
        d[-1][1] += d[-2][3] * 45
        d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45
    d[-1][0] %= C
    d[-1][1] %= C
    d[-1][2] %= C
    d[-1][3] %= C
print(d[-1][2])

033

import math
dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)]
n, m, k = list(map(int, input().split()))
k += 1
z = list(map(int, input().split()))
matrix = []
for i in range(n):
    ans = list(map(int, input().split()))
    matrix.append(ans)
for i in range(len(z)):
    if i == 0:
        if z[i] != 0:
            dp[0][1][z[i] - 1] = 0
        else:
            for x in range(m):
                dp[0][1][x] = matrix[i][x]
    elif z[i] != 4:
        col = z[i] - 1
        if z[i - 1] != 0:
            col2 = z[i - 1] - 1
            if col2 == col:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col])
            else:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col])
        else:
            pq = []
            for t in range(k):
                if t == 0:
                    continue
                pq = []
                for x in range(m):
                    pq.append([dp[i - 1][t - 1][x], x])
                pq.append([math.inf, math.inf])
                pq.sort()
                col = z[i] - 1
                if col != pq[0][1] and pq[0][0] != math.inf:
                    dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col])
                elif pq[0][0] != math.inf and col == pq[0][1]:
                    dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col])
                elif pq[0][0] == math.inf:
                    dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col])
    elif z[i - 1] != 0:
        col = z[i - 1] - 1
        for t in range(k):
            if t == 0:
                continue
            for x in range(m):
                if x != col:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x])
                else:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x])
    else:
        for t in range(k):
            if t == 0:
                continue
            pq = []
            for x in range(m):
                pq.append([dp[i - 1][t - 1][x], x])
            pq.append([math.inf, math.inf])
            pq.sort()
            for v in range(m):
                if v != pq[0][1]:
                    dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v])
                else:
                    dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v])
mini = math.inf
for i in range(m):
    mini = min(mini, dp[n - 1][k - 1][i])
if mini == math.inf:
    print(-1)
else:
    print(mini)

034

n, v = list(map(int, input().split()))
f = list()
for i in range(n):
    f.append(list(map(int, input().split())))
f.sort()
sum = 0
today = 0
tomor = 0
day = 0
rest = 0
for tree in f:
    if tree[0] > day + 5:
        sum += min(tomor, v)
        tomor = tree[1]
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor -= min(tomor, v)
        day = tree[0]
    elif tree[0] == day + 1:
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor = tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
        day = tree[0]
    else:
        tomor += tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
sum += min(tomor, v)
print(sum)

035

n, v = list(map(int, input().split()))
f = list()
for i in range(n):
    f.append(list(map(int, input().split())))
f.sort()
sum = 0
today = 0
tomor = 0
day = 0
rest = 0
for tree in f:
    if tree[0] > day + 1:
        sum += min(tomor, v)
        tomor = tree[0]
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor -= min(tomor, v)
        day = tree[0]
    elif tree[0] == day + 1:
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor = tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
        day = tree[0]
    else:
        tomor += tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
sum += min(tomor, v)
print(sum)

036

m, t, r = input().split(sep=None, maxsplit=300)
m, T, r = (int(m), int(t), int(r))
x = input().split(sep=None, maxsplit=m)
k = r
out = set([])
u = [int(x[0]) + T - 1 - i for i in range(r)]
out |= set(u)
key = True
for i in range(m):
    if not key:
        break
    v = int(x[i])
    t = v + T - 1
    for j in range(r):
        if v <= u[j]:
            continue
        while t in out and t >= v:
            t -= 1
        if t < v:
            key = False
            break
        else:
            out |= set([t])
            u[j] = t
            k += 1
            t -= 2
if key:
    print(k)
else:
    print(-1)

037

m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - -1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - 1 - x for x in range(0, r)]
res = r
for i in range(0, m):
    for j in range(0, r):
        candleTime = candles[j]
        if candleTime < ghosts[i]:
            possibleTime = ghosts[i] - 1
            canFindTime = False
            while possibleTime >= ghosts[i] - t:
                if usedTimes.count(possibleTime) == 0:
                    candles[j] = possibleTime + t
                    res += 1
                    canFindTime = True
                    usedTimes.append(possibleTime)
                    break
                else:
                    possibleTime -= 1
            if not canFindTime:
                res = -1
                break
    if res == -1:
        break
print(res)

038

n, m, k = map(int, input().split())
col = list(map(int, input().split()))
p = []
for i in range(n):
    p.append(list(map(int, input().split())))
dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)]
if col[0] == 0:
    for i in range(m):
        dp[0][1][i] = p[0][i]
else:
    dp[0][1][col[0] - 1] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if j == 1:
            if col[i]:
                dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1]
            else:
                for c in range(m):
                    dp[i][j][c] = dp[i - 1][j][c] + p[i][c]
        elif col[i]:
            x = dp[i - 1][j][col[i] - 1]
            for t in range(m):
                if t != col[i] - 1:
                    x = min(x, dp[i - 1][j - 1][t])
            dp[i][j][col[i] - 1] = x
        else:
            for c in range(m):
                x = dp[i - 1][j][c]
                for t in range(m):
                    if t != c:
                        x = min(x, dp[i - 1][j - 1][t])
                dp[i][j][c] = x + p[i][c]
ans = min(dp[-1][-1])
if ans != float('inf'):
    print(ans)
else:
    print(-3)

039

import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
    if meches[i] > l:
        trop_grand[i] = True
        if i == 0 or trop_grand[i - 1] is False:
            pont += 1
for i in range(nbreq):
    req = sys.stdin.readline().split()
    if int(req[0]) == 0:
        print(pont)
    else:
        meches[int(req[1]) - 1] += int(req[2])
        if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
            trop_grand[int(req[1]) - 1] = True
            if nbmeches == 1:
                pont = 1
            elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
                pont += -1
            elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
                pont += 1
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
                pont = max(0, pont - 1)
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
                pont += 1

040

class DisjoinSet:

    def __init__(self, n):
        self.n = n
        self.dad = [i for i in range(n + 1)]
        self.cant = [1] * (n + 1)

    def SetOf(self, n):
        if self.dad[n] == n:
            return n
        self.dad[n] = self.SetOf(self.dad[n])
        return self.dad[n]

    def Merge(self, a, b):
        a, b = (self.SetOf(a), self.SetOf(b))
        if a != b:
            if self.cant[b] > self.cant[a]:
                a, b = (b, a)
            self.cant[a] += self.cant[b]
            self.dad[b] = a
            return True
        return False

def Kruskal(l, n):
    l.sort()
    i = 0
    ds = DisjoinSet(n)
    cant = 0
    change = 0
    while cant < n - 1 and i < len(l):
        val = l[i][0]
        old = []
        while i < len(l) and val == l[i][0]:
            x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2]))
            if x != y:
                old.append((x, y))
            i += 1
        change += len(old)
        for x, y in old:
            change -= ds.Merge(x, y)
    return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
    x, y, c = input().split()
    e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))

041

n = int(input())
C = 10 ** 9 + 7
a = input()
s = input()
d = [[0, 0, 0, 1]]
for q in range(n):
    d.append(d[-1][:])
    if a[q] != '?' and s[q] != '?':
        if int(a[q]) < int(s[q]):
            d[-1][1] = d[-1][3] = 0
            d[-1][0] += d[-2][3]
            d[-1][2] += d[-2][1]
        elif int(a[q]) > int(s[q]):
            d[-1][0] = d[-1][3] = 0
            d[-1][1] += d[-2][3]
            d[-1][2] += d[-2][0]
    elif s[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][0] *= int(s[q]) + 1
        d[-1][1] *= 10 - int(s[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q])
    elif a[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][1] *= int(a[q]) + 1
        d[-1][0] *= 10 - int(a[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q])
    else:
        d[-1][3] *= 10
        d[-1][2] *= 100
        d[-1][0] *= 55
        d[-1][1] *= 55
        d[-1][0] += d[-2][3] * 45
        d[-1][1] += d[-2][3] * 45
        d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45
    d[-1][0] %= C
    d[-1][1] %= C
    d[-1 - 1][2] %= C
    d[-1][3] %= C
print(d[-1][2])

042

def main():
    n, m, k = list(map(int, input().split()))
    c, *cc = list(map(int, input().split()))
    pp, *ppp = (list(map(float, input().split())) for _ in range(n))
    inf = 2.0 ** 47
    nxt = [[0.0 if i == c - -1 else inf for i in range(m)] if c else pp]
    for c, pp in zip(cc, ppp):
        newrow = [inf] * m
        cur, nxt = (nxt, [newrow])
        if c:
            c -= 1
            for row in cur:
                p = row[c]
                if newrow[c] > p:
                    newrow[c] = p
                if len(nxt) == k:
                    break
                row[c] = inf
                newrow = [inf] * m
                newrow[c] = min(row)
                nxt.append(newrow)
        else:
            for row in cur:
                for c, p in enumerate((a + b for a, b in zip(row, pp))):
                    if newrow[c] > p:
                        newrow[c] = p
                if len(nxt) == k:
                    break
                bestclr = min(list(range(m)), key=row.__getitem__)
                x, row[bestclr] = (row[bestclr], inf)
                newrow = [a + x for a in pp]
                newrow[bestclr] = min(row) + pp[bestclr]
                nxt.append(newrow)
    p = min(nxt[-1])
    print(int(p) if p < inf else -1)

def __starting_point():
    main()
__starting_point()

043

n, m, k = map(int, input().split())
col = list(map(int, input().split()))
p = []
for i in range(n):
    p.append(list(map(int, input().split())))
dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)]
if col[0] == 0:
    for i in range(m):
        dp[0][1][i] = p[0][i]
else:
    dp[0][1][col[0] - 1] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if j == 1:
            if col[i - 1]:
                dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1]
            else:
                for c in range(m):
                    dp[i][j][c] = dp[i - 1][j][c] + p[i][c]
        elif col[i]:
            x = dp[i - 1][j][col[i] - 1]
            for t in range(m):
                if t != col[i] - 1:
                    x = min(x, dp[i - 1][j - 1][t])
            dp[i][j][col[i] - 1] = x
        else:
            for c in range(m):
                x = dp[i - 1][j][c]
                for t in range(m):
                    if t != c:
                        x = min(x, dp[i - 1][j - 1][t])
                dp[i][j][c] = x + p[i][c]
ans = min(dp[-1][-1])
if ans != float('inf'):
    print(ans)
else:
    print(-1)

044

n = int(input())
a = input()
b = input()
mod = int(1000000000.0 + 7)
x, y, z = (1, 1, 1)
for i in range(n):
    if a[i] == '?' and b[i] == '?':
        x = x * 55 % mod
        y = y * 55 % mod
        z = z * 10 % mod
    elif a[i] == '?':
        x = x * (10 - int(b[i])) % mod
        y = y * (int(b[i]) + 1) % mod
    elif b[i] == '?':
        x = x * (int(a[i]) + 1) // mod
        y = y * (10 - int(a[i])) % mod
    else:
        if int(a[i]) < int(b[i]):
            x = 0
        if int(a[i]) > int(b[i]):
            y = 0
        if a[i] != b[i]:
            z = 0
res = pow(10, a.count('?') + b.count('?'), mod)
print((res - x - y + z) % mod)

045

def main():
    from collections import deque
    n, m = list(map(int, input().split()))
    aa = list(map(int, input().split()))
    l = list((tuple(map(int, input().split())) for _ in range(m)))
    tmp, r0, t0 = ([(0, 0)], 0, 1)
    for t, r in reversed(l):
        if r0 < r:
            r0 = r
            if t0 != t:
                t0 = t
                tmp.append((t, r))
            else:
                tmp[-1] = (t, r)
    t0, r0 = tmp.pop()
    q = deque(sorted(aa[:r0]))
    aa = aa[r0:]
    aa.reverse()
    ff = (None, q.pop, q.popleft)
    for t, r in reversed(tmp):
        f = ff[t0]
        for i in range(r0 - r):
            aa.append(f())
        t0, r0 = (t, r)
    print(' '.join(map(str, reversed(aa))))

def __starting_point():
    main()
__starting_point()

046

import sys

def get(v):
    if v == prev[v]:
        return v
    prev[v] = get(prev[v])
    return prev[v]

def join(u, v):
    u = get(u)
    v = get(v)
    if u != v:
        if size[u] < size[v]:
            u, v = (v, u)
        prev[v] = u
        size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
    u, v, w = map(int, sys.stdin.readline().split())
    edges[i] = (u - 1, v - 1, w)
edges.sort(key=lambda e: e[-2])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
    while r < m and edges[l][2] == edges[r][2]:
        r += 1
    res += r - l
    for i in range(l, r):
        if get(edges[i][0]) == get(edges[i][1]):
            res -= 1
    for i in range(l, r):
        if get(edges[i][0]) != get(edges[i][1]):
            join(edges[i][0], edges[i][1])
            res -= 1
    l = r
print(res)

047

MAX = 10 ** 18 + 5
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
cost = [list(map(int, input().split())) for _ in range(n)]
min1 = [[MAX] * (k + 1) for _ in range(n)]
min2 = [[MAX] * (k + 1) for _ in range(n)]
idx = [[-1] * (k + 1) for _ in range(n)]
for i in cost:
    i.insert(0, 0)
dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)]
if a[0] == 0:
    for i in range(2, m + 1):
        if min1[0][1] >= cost[0][i]:
            if min1[0][1] == cost[0][i]:
                idx[0][1] = -2
            else:
                idx[0][1] = i
            min2[0][1] = min1[0][1]
            min1[0][1] = cost[0][i]
        elif min2[0][1] >= cost[0][i]:
            min2[0][1] = cost[0][i]
        dp[0][1][i] = cost[0][i]
else:
    dp[0][1][a[0]] = 0
    min1[0][1] = 0
    idx[0][1] = a[0]
for i in range(1, n):
    for j in range(1, k + 1):
        if a[i] == 0:
            for l in range(1, m + 1):
                dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l])
                if l == idx[i - 1][j - 1]:
                    dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l])
                else:
                    dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l])
        else:
            for l in range(1, m + 1):
                dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]])
                if l != a[i]:
                    dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]])
        for l in range(1, m + 1):
            if min1[i][j] >= dp[i][j][l]:
                if min1[i][j] == dp[i][j][l]:
                    idx[i][j] = -2
                else:
                    idx[i][j] = l
                min2[i][j] = min1[i][j]
                min1[i][j] = dp[i][j][l]
            elif min2[i][j] >= dp[i][j][l]:
                min2[i][j] = dp[i][j][l]
ans = MAX
for i in dp[-1][-1]:
    ans = min(ans, i)
print(ans if ans < MAX else -1)

048

n, m, l = list(map(int, input().split()))
l += 1
hair = [1] * (n + 1)
other_end = [1] * (n + 1)
i = 1
j = 1
big = 1
res = 1
for a in map(int, input().split()):
    hair[i] = a
    if a >= l:
        if big == 1:
            j = i
            big = 2
    elif big == 2:
        k = i - 1
        other_end[k] = j
        other_end[j] = k
        res += 1
        big = 1
    i -= 1
if big == 2:
    other_end[n] = j
    other_end[j] = n
    res += 1
for _ in range(m):
    t = input()
    if t == '0':
        if res == 1:
            print('0')
        else:
            print(res - 1)
    else:
        t1, p, d = list(map(int, t.split()))
        old = hair[p]
        hair[p] += d
        if hair[p] >= l > old:
            if p == 1 or hair[p - 1] < l:
                case_id = 1
            else:
                case_id = 2
            if p != n and hair[p + 1] >= l:
                case_id += 2
            if case_id == 1:
                other_end[p] = p
                res += 1
            elif case_id == 2:
                other_end[p] = other_end[p - 1]
                other_end[other_end[p]] = p
            elif case_id == 3:
                other_end[p] = other_end[p + 1]
                other_end[other_end[p]] = p
            else:
                left_end = other_end[p - 1]
                right_end = other_end[p + 1]
                other_end[left_end] = right_end
                other_end[right_end] = left_end
                res -= 1

049

w, h, n = map(int, input().split())
l = [-1] * (w + 1)
r = [-1] * (w + 1)
t = [-1] * (h + 1)
b = [-1] * (h + 1)
l[0] = 0
b[0] = 0
t[h] = h
r[w] = w
V = [0] * n
H = [0] * n
for i in range(n):
    line, index = input().split()
    index = int(index)
    if line == 'V':
        r[index] = w
        V[i] = index
    else:
        t[index] = h
        H[i] = index
left = 0
mxw = 0
for i in range(1, w + 1 - 1):
    if r[i] != -1:
        l[i] = left
        r[left] = i
        mxw = max(mxw, i - left)
        left = i
bottom = 0
mxh = 0
for i in range(1, h + 1):
    if t[i] != -1:
        b[i] = bottom
        t[bottom] = i
        mxh = max(mxh, i - bottom)
        bottom = i
ans = [0] * n
ans[n - 1] = mxh * mxw
for i in range(n - 1, 0, -1):
    if V[i] != 0:
        mxw = max(mxw, r[V[i]] - l[V[i]])
        r[l[V[i]]] = r[V[i]]
        l[r[V[i]]] = l[V[i]]
    else:
        mxh = max(mxh, t[H[i]] - b[H[i]])
        b[t[H[i]]] = b[H[i]]
        t[b[H[i]]] = t[H[i]]
    ans[i - 1] = mxh * mxw
for i in range(n):
    print(ans[i])

050

import math
dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)]
n, m, k = list(map(int, input().split()))
k += 1
z = list(map(int, input().split()))
matrix = []
for i in range(n):
    ans = list(map(int, input().split()))
    matrix.append(ans)
for i in range(len(z)):
    if i == 0:
        if z[i] != 0:
            dp[0][1][z[i] - 1] = 0
        else:
            for x in range(m):
                dp[0][1][x] = matrix[i][x]
    elif z[i] != 0:
        col = z[i] - 1
        if z[i - 1] != 0:
            col2 = z[i - 1] + 1
            if col2 == col:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col])
            else:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col])
        else:
            pq = []
            for t in range(k):
                if t == 0:
                    continue
                pq = []
                for x in range(m):
                    pq.append([dp[i - 1][t - 1][x], x])
                pq.append([math.inf, math.inf])
                pq.sort()
                col = z[i] - 1
                if col != pq[0][1] and pq[0][0] != math.inf:
                    dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col])
                elif pq[0][0] != math.inf and col == pq[0][1]:
                    dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col])
                elif pq[0][0] == math.inf:
                    dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col])
    elif z[i - 1] != 0:
        col = z[i - 1] - 1
        for t in range(k):
            if t == 0:
                continue
            for x in range(m):
                if x != col:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x])
                else:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x])
    else:
        for t in range(k):
            if t == 0:
                continue
            pq = []
            for x in range(m):
                pq.append([dp[i - 1][t - 1][x], x])
            pq.append([math.inf, math.inf])
            pq.sort()
            for v in range(m):
                if v != pq[0][1]:
                    dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v])
                else:
                    dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v])
mini = math.inf
for i in range(m):
    mini = min(mini, dp[n - 1][k - 1][i])
if mini == math.inf:
    print(-1)
else:
    print(mini)

051

MAX_SPEED = 300
n = int(input())
actions = [list(map(int, input().split())) for _ in range(n)]
speed = [0 for i in range(n)]
for i in range(n):
    if actions[i][0] == 1:
        lastSpeed = actions[i][1]
    speed[i] = lastSpeed
ignored = 0
overtook = False
maxSpeed = -1
for i in range(n - 1, -1, -1):
    maxSpeed = max(maxSpeed, speed[i])
    if actions[i][0] == 1:
        pass
    elif actions[i][0] == 5:
        overtook = True
    elif actions[i][0] == 3:
        if maxSpeed > actions[i][1]:
            ignored += 1
        else:
            maxSpeed = speed[i - 1]
    elif actions[i][0] == 4:
        overtook = False
    elif actions[i][0] == 5:
        maxSpeed = speed[i - 1]
    elif actions[i][0] == 6:
        if overtook:
            ignored += 1
print(ignored)

052

n = int(input())
C = 7 ** 9 + 7
a = input()
s = input()
d = [[0, 0, 0, 1]]
for q in range(n):
    d.append(d[-1][:])
    if a[q] != '?' and s[q] != '?':
        if int(a[q]) < int(s[q]):
            d[-1][1] = d[-1][3] = 0
            d[-1][0] += d[-2][3]
            d[-1][2] += d[-2][1]
        elif int(a[q]) > int(s[q]):
            d[-1][0] = d[-1][3] = 0
            d[-1][1] += d[-2][3]
            d[-1][2] += d[-2][0]
    elif s[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][0] *= int(s[q]) + 1
        d[-1][1] *= 10 - int(s[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q])
    elif a[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][1] *= int(a[q]) + 1
        d[-1][0] *= 10 - int(a[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q])
    else:
        d[-1][3] *= 10
        d[-1][2] *= 100
        d[-1][0] *= 55
        d[-1][1] *= 55
        d[-1][0] += d[-2][3] * 45
        d[-1][1] += d[-2][3] * 45
        d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45
    d[-1][0] %= C
    d[-1][1] %= C
    d[-1][2] %= C
    d[-1][3] %= C
print(d[-1][2])

053

import math
from collections import Counter, defaultdict
from itertools import accumulate
R = lambda: map(int, input().split())
n = int(input())
spl = [math.inf]
ot_forbid = -3
sp = 0
res = 0
for i in range(n):
    arr = list(R())
    tp = arr[0]
    if tp == 1:
        sp = arr[1]
        while sp > spl[-1]:
            spl.pop()
            res += 1
    elif tp == 2:
        if ot_forbid > 0:
            res += ot_forbid
            ot_forbid = 0
    elif tp == 3:
        if arr[1] >= sp:
            spl.append(arr[1])
        else:
            res += 1
    elif tp == 4:
        ot_forbid = 0
    elif tp == 5:
        spl = [math.inf]
    else:
        ot_forbid += 1
print(res)

054

total_plucked = 0
n, v = list(map(int, input().split()))
fruits = [0] * 3002
plucked = [0] * 30002
for _ in range(n):
    a, b = list(map(int, input().split()))
    fruits[a] = fruits[a] + b
for i in range(1, 3002):
    plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v
    if plucked[i] < v:
        pluck = fruits[i] if fruits[i] <= v + plucked[i] else v - plucked[i]
        plucked[i] += pluck
        fruits[i] -= pluck
    total_plucked += plucked[i]
print(total_plucked)

055

3

def build(n, s, t):
    ans = 1
    for i in range(n):
        if s[i] == '?' and t[i] == '?':
            ans = 55 * ans % (10 ** 9 + 7)
        elif s[i] == '?':
            ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7)
        elif t[i] == '?':
            ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7)
    return ans
n = int(input())
s = input()
t = input()
sltt = True
tlts = True
qm = 0
cqm = 0
for i in range(n):
    if t[i] == '?':
        qm += 1
    if s[i] == '?':
        qm += 1
    if t[i] == '?' and s[i] == '?':
        cqm += 1
    if t[i] == '?' and s[i] == '?':
        continue
    if ord(s[i]) < ord(t[i]):
        tlts = False
    if ord(t[i]) < ord(s[i]):
        sltt = False
if not sltt and (not tlts):
    print(pow(10, qm, 10 ** 9 + 7))
elif sltt and tlts:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 + 7))
elif sltt:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7))
else:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7))

056

import sys
import math
n, v = [int(x) for x in sys.stdin.readline().split()]
k = [0] * 3002
for i in range(n):
    a, b = [int(x) for x in sys.stdin.readline().split()]
    k[a] += b
i = 1
res = 0
while i <= 3001:
    val = 0
    if k[i - 1] < v:
        val = k[i - 1]
        res += val
        d = v - val
        if k[i] < d:
            res += k[i]
            k[i] = 0
        else:
            res += d
            k[i] = k[i] * d
    else:
        res += v
    i += 1
print(res)

057

n, m, l = list(map(int, input().split()))
l += 1
hair = [1] * (n + 1)
other_end = [1] * (n + 1)
i = 1
j = 1
big = 1
res = 1
for a in map(int, input().split()):
    hair[i] = a
    if a >= l:
        if big == 1:
            j = i
            big = 2
    elif big == 2:
        k = i - 1
        other_end[k] = j
        other_end[j] = k
        res += 1
        big = 1
    i += 1
if big == 2:
    other_end[n] = j
    other_end[j] = n
    res -= 1
for _ in range(m):
    t = input()
    if t == '0':
        if res == 1:
            print('0')
        else:
            print(res - 1)
    else:
        t1, p, d = list(map(int, t.split()))
        old = hair[p]
        hair[p] += d
        if hair[p] >= l > old:
            if p == 1 or hair[p - 1] < l:
                case_id = 1
            else:
                case_id = 2
            if p != n and hair[p + 1] >= l:
                case_id += 2
            if case_id == 1:
                other_end[p] = p
                res += 1
            elif case_id == 2:
                other_end[p] = other_end[p - 1]
                other_end[other_end[p]] = p
            elif case_id == 3:
                other_end[p] = other_end[p + 1]
                other_end[other_end[p]] = p
            else:
                left_end = other_end[p - 1]
                right_end = other_end[p + 1]
                other_end[left_end] = right_end
                other_end[right_end] = left_end
                res -= 1

058

def main():
    m, t, r = read()
    w = read()
    if t < r:
        return -1
    covers = [0] * 1000
    ct = 0
    for i in w:
        x = covers[i]
        for j in range(r - x):
            for k in range(t):
                covers[i - j + k] += 2
            ct += 1
    return ct

def read(mode=2):
    inputs = input().strip()
    if mode == 0:
        return inputs
    if mode == 1:
        return inputs.split()
    if mode == 2:
        return list(map(int, inputs.split()))

def write(s='\n'):
    if s is None:
        s = ''
    if isinstance(s, list):
        s = ' '.join(map(str, s))
    s = str(s)
    print(s, end='')
write(main())

059

def main():
    m, t, r = list(map(int, input().split()))
    gh = sorted(map(int, input().split()), reverse=True)
    if r > t:
        print(-1)
        return
    l = [False] * 600
    for g in gh:
        g -= t
        for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
            l[i] = True
    print(sum(l))

def __starting_point():
    main()
__starting_point()

060

def Task485D():
    cur_speed = 0
    is_outstrip = 0
    err_count = 0
    event_speed_limit = []
    event_count = int(input())
    for a in range(event_count):
        ev = list(map(int, input().split()))
        if ev[0] == 1:
            cur_speed = ev[1]
            while len(event_speed_limit) > 0:
                if cur_speed > event_speed_limit[len(event_speed_limit) - 1]:
                    event_speed_limit.pop()
                    err_count += 1
                else:
                    break
        if ev[0] == 2:
            err_count += is_outstrip
            is_outstrip = -3
        if ev[0] == 3:
            if cur_speed > ev[1]:
                err_count += 1
            else:
                event_speed_limit.append(ev[1])
        if ev[0] == 4:
            is_outstrip = 0
        if ev[0] == 5:
            event_speed_limit.clear()
        if ev[0] == 6:
            is_outstrip += 1
    print(err_count)

def __starting_point():
    Task485D()
__starting_point()

061

m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - 1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - 1 - x for x in range(0, r)]
res = r
for i in range(0, m):
    for j in range(0, r):
        candleTime = candles[j]
        if candleTime <= ghosts[i]:
            possibleTime = ghosts[i] - 1
            canFindTime = False
            while possibleTime >= ghosts[i] - t:
                if usedTimes.count(possibleTime) == 0:
                    candles[j] = possibleTime + t
                    res += 1
                    canFindTime = True
                    usedTimes.append(possibleTime)
                    break
                else:
                    possibleTime -= 1
            if not canFindTime:
                res = -1
                break
    if res == -1:
        break
print(res)

062

n, v = list(map(int, input().split()))
f = list()
for i in range(n):
    f.append(list(map(int, input().split())))
f.sort()
sum = 0
today = 0
tomor = 0
day = 0
rest = 0
for tree in f:
    if tree[0] >= day + 1:
        sum += min(tomor, v)
        tomor = tree[1]
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor -= min(tomor, v)
        day = tree[0]
    elif tree[0] == day + 1:
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor = tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
        day = tree[0]
    else:
        tomor += tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
sum += min(tomor, v)
print(sum)

063

n, m, l = list(map(int, input().split()))
l += 1
hair = [1] * (n + 1)
other_end = [1] * (n + 1)
i = 1
j = 1
big = 1
res = 1
for a in map(int, input().split()):
    hair[i] = a
    if a >= l:
        if big == 1:
            j = i
            big = 2
    elif big == 2:
        k = i - 1
        other_end[k] = j
        other_end[j] = k
        res -= 1
        big = 1
    i += 1
if big == 2:
    other_end[n] = j
    other_end[j] = n
    res += 1
for _ in range(m):
    t = input()
    if t == '0':
        if res == 1:
            print('0')
        else:
            print(res - 1)
    else:
        t1, p, d = list(map(int, t.split()))
        old = hair[p]
        hair[p] += d
        if hair[p] >= l > old:
            if p == 1 or hair[p - 1] < l:
                case_id = 1
            else:
                case_id = 2
            if p != n and hair[p + 1] >= l:
                case_id += 2
            if case_id == 1:
                other_end[p] = p
                res += 1
            elif case_id == 2:
                other_end[p] = other_end[p - 1]
                other_end[other_end[p]] = p
            elif case_id == 3:
                other_end[p] = other_end[p + 1]
                other_end[other_end[p]] = p
            else:
                left_end = other_end[p - 1]
                right_end = other_end[p + 1]
                other_end[left_end] = right_end
                other_end[right_end] = left_end
                res -= 1

064

import sys
n, m = [int(x) for x in input().split()]
notes = [int(x) for x in input().split()]
managers = []
for line in sys.stdin:
    managers.append(tuple((int(x) for x in line.split())))
bearing_managers = []
M, T = (0, 0)
for t, r in reversed(managers):
    if r > M:
        if t == T:
            bearing_managers[-1] = (t, r)
        else:
            T = t
            bearing_managers.append((t, r))
        M = r
bearing_managers.reverse()
t, r = bearing_managers[0]
bearing_managers.append((-1 if bearing_managers[-1][0] == 1 else 1, 0))
L, R = (0, r - 1)
sorted_part = list(sorted(notes[:r]))
for t1, r1 in bearing_managers:
    if t1 == 2:
        for i in range(r - 1, r1 - 1, -1):
            notes[i] = sorted_part[R]
            R -= 1
    else:
        for i in range(r - 1, r1 - 1, -1):
            notes[i] = sorted_part[L]
            L += 1
    r = r1
for x in notes:
    sys.stdout.write(str(x) + ' ')

065

w, h, n = map(int, input().split())
x = [0, w]
y = [0, h]
rev = []
for _ in range(n):
    s, d = input().split()
    if s == 'H':
        y.append(int(d))
    else:
        x.append(int(d))
    rev.append((s, int(d)))
x.sort()
y.sort()
_max = 0
if len(x) > 1:
    for idx in range(len(x) - 1):
        _max = max(_max, x[idx + 1] - x[idx])
else:
    _max = w
max_x = _max
_max = 0
if len(y) > 1:
    for idx in range(len(y) - 1):
        _max = max(_max, y[idx + 1] - y[idx])
else:
    _max = w
max_y = _max
enum_x = {num: idx for idx, num in enumerate(x)}
enum_y = {num: idx for idx, num in enumerate(y)}
old_x = x
old_y = y
x = [[0, 0, 0]] * len(old_x)
y = [[0, 0, 0]] * len(old_y)
for idx in range(1, len(x) - 1):
    x[idx] = [old_x[idx], idx - 1, idx + 1]
for idx in range(1, len(y) - 1):
    y[idx] = [old_y[idx], idx - 1, idx + 1]
x[-1 - 1] = [w, 0, 0]
y[-1] = [h, 0, 0]
rev.reverse()
ans = [max_x * max_y]
for item in rev:
    if item[0] == 'H':
        elem = y[enum_y[item[1]]]
        max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0])
        y[elem[1]][2] = elem[2]
        y[elem[2]][1] = elem[1]
    else:
        elem = x[enum_x[item[1]]]
        max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0])
        x[elem[1]][2] = elem[2]
        x[elem[2]][1] = elem[1]
    ans.append(max_x * max_y)
ans.pop()
print('\n'.join(map(str, reversed(ans))))

066

a, b = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
s = []
for p in range(b):
    s += [[int(i) for i in input().split()]]
S = []
k = 0
for p in range(b - 1, -1, -1):
    if s[p][1] > k:
        S += [s[p]]
        k = s[p][1]
Q1 = sorted(q[:S[-1][1]])
x, y = (0, S[-1][1] - 1)
S = [[0, 0]] + S
l = 1
for j in range(S[-1][1] - 1, -1, -1):
    if x > y:
        break
    if j < S[-l][1]:
        k = S[-l][0]
        l += -1
    if k == 1:
        q[j] = Q1[y]
        y -= 1
    if k == 2:
        q[j] = Q1[x]
        x += 1
print(' '.join(list(map(str, q))))

067

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

class Node:
    val = None

    def __init__(self, val):
        self.val = val
        self.left = Node
        self.right = None

def solve(W, H, N, A):
    xs = [0] + [v for t, v in A if t == 0] + [W]
    ys = [0] + [v for t, v in A if t == 1] + [H]
    xs.sort()
    ys.sort()
    xlist = Node(3)
    h = xlist
    xnodes = {0: h}
    maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0])
    maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0])
    for v in xs[1:]:
        n = Node(v)
        xnodes[v] = n
        h.right = n
        n.left = h
        h = n
    ylist = Node(0)
    h = ylist
    ynodes = {0: h}
    for v in ys[1:]:
        n = Node(v)
        ynodes[v] = n
        h.right = n
        n.left = h
        h = n
    ans = []
    maxarea = maxh * maxw
    for t, v in reversed(A):
        ans.append(maxarea)
        if t == 0:
            node = xnodes[v]
            w = node.right.val - node.left.val
            maxw = max(maxw, w)
        else:
            node = ynodes[v]
            h = node.right.val - node.left.val
            maxh = max(maxh, h)
        node.left.right = node.right
        node.right.left = node.left
        maxarea = maxh * maxw
    return ans[::-1]

def solve2(W, H, N, A):
    ws = [(-W, 0, W)]
    hs = [(-H, 0, H)]
    iw, ih = (set(), set())
    ans = []
    xs, ys = ([0, W], [0, H])
    for t, v in A:
        if t == 0:
            bisect.insort_left(xs, v)
            i = bisect.bisect_left(xs, v)
            l, m, r = (xs[i - 1], xs[i], xs[i + 1])
            iw.add((l - r, l, r))
            heapq.heappush(ws, (l - m, l, m))
            heapq.heappush(ws, (m - r, m, r))
            while ws[0] in iw:
                heapq.heappop(ws)
        else:
            bisect.insort(ys, v)
            i = bisect.bisect_left(ys, v)
            l, m, r = (ys[i - 1], ys[i], ys[i + 1])
            ih.add((l - r, l, r))
            heapq.heappush(hs, (l - m, l, m))
            heapq.heappush(hs, (m - r, m, r))
            while hs[0] in ih:
                heapq.heappop(hs)
        w, h = (ws[0], hs[0])
        ans.append(w[0] * h[0])
    return ans
W, H, N = map(int, input().split())
A = []
for i in range(N):
    a, b = input().split()
    c = 0 if a == 'V' else 1
    A.append((c, int(b)))
print('\n'.join(map(str, solve(W, H, N, A))))

068

w, h, n = map(int, input().split())
l = [-1] * (w + 1)
r = [-1] * (w + 1)
t = [-1] * (h + 1)
b = [-1] * (h + 1)
l[0] = 0
b[0] = 0
t[h] = h
r[w] = w
V = [0] * n
H = [0] * n
for i in range(n):
    line, index = input().split()
    index = int(index)
    if line == 'V':
        r[index] = w
        V[i] = index
    else:
        t[index] = h
        H[i] = index
left = 0
mxw = 0
for i in range(2, w + 1):
    if r[i] != -1:
        l[i] = left
        r[left] = i
        mxw = max(mxw, i - left)
        left = i
bottom = 0
mxh = 0
for i in range(1, h + 1):
    if t[i] != -1:
        b[i] = bottom
        t[bottom] = i
        mxh = max(mxh, i - bottom)
        bottom = i
ans = [0] * n
ans[n - 1] = mxh * mxw
for i in range(n - 1, 0, -1):
    if V[i] != 0:
        mxw = max(mxw, r[V[i]] - l[V[i]])
        r[l[V[i]]] = r[V[i]]
        l[r[V[i]]] = l[V[i]]
    else:
        mxh = max(mxh, t[H[i]] - b[H[i]])
        b[t[H[i]]] = b[H[i]]
        t[b[H[i]]] = t[H[i]]
    ans[i - 1] = mxh * mxw
for i in range(n):
    print(ans[i])

069

def main():
    from collections import deque
    n, m = list(map(int, input().split()))
    aa = list(map(int, input().split()))
    l = list((tuple(map(int, input().split())) for _ in range(m)))
    tmp, r0, t0 = ([(0, 0)], 0, 0)
    for t, r in reversed(l):
        if r0 < r:
            r0 = r
            if t0 != t:
                t0 = t
                tmp.append((t, r))
            else:
                tmp[-1] = (t, r)
    t0, r0 = tmp.pop()
    q = deque(sorted(aa[:r0]))
    aa = aa[r0:]
    aa.reverse()
    ff = (None, q.pop, q.popleft)
    for t, r in reversed(tmp):
        f = ff[t0]
        for i in range(r0 - r):
            aa.append(f())
        t0, r0 = (t, r)
    print(' '.join(map(str, reversed(aa))))

def __starting_point():
    main()
__starting_point()

070

from functools import reduce
n = int(input())
s1, s2 = (str(input()), str(input()))
b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False)
b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False)
s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)])
ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1)
ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1)
ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1)
print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000003)

071

import sys
n, k = list(map(int, input().split(' ')))
activate = list(map(int, input().split(' ')))
deactivate = list(map(int, input().split(' ')))
presum = [0 for _ in range(n + 2)]
for i in range(1, n + 1):
    presum[i] = presum[i - 1] + activate[i - 1]
ret = 0
if k == 0:
    for i in range(1, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
elif k == 1:
    for i in range(1, n):
        ret = max(ret, presum[n - 1] - deactivate[i - 1])
    if deactivate[n - 1] < activate[n - 1]:
        ret += activate[n - 1] - deactivate[n - 1]
    deac = sorted(deactivate[1 - 1:n - 1])
    ac = sorted(activate[1:n - 1])
    ret = max(ret, presum[n] - deactivate[0] - ac[0])
    ret = max(ret, presum[n] - deactivate[0] - deac[0])
    ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1])
    for i in range(2, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
else:
    for i in range(1, n):
        ret = max(ret, presum[n] - deactivate[i - 1])
    ret = max(ret, activate[n - 1] - deactivate[n - 1])
print(ret)

072

MAX = 10 ** 18 + 5
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
cost = [list(map(int, input().split())) for _ in range(n)]
min1 = [[MAX] * (k + 1) for _ in range(n)]
min2 = [[MAX] * (k + 1) for _ in range(n)]
idx = [[-1] * (k + 1) for _ in range(n)]
for i in cost:
    i.insert(0, 0)
dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)]
if a[0] == 0:
    for i in range(1, m + 1):
        if min1[0][1] >= cost[0][i]:
            if min1[0][1] == cost[0][i]:
                idx[0][1] = -2
            else:
                idx[0][1] = i
            min2[0][1] = min1[0][1]
            min1[0][1] = cost[0][i]
        elif min2[0][1] >= cost[0][i]:
            min2[0][1] = cost[0][i]
        dp[0][1][i] = cost[0][i]
else:
    dp[0][1][a[0]] = 0
    min1[0][-1] = 0
    idx[0][1] = a[0]
for i in range(1, n):
    for j in range(1, k + 1):
        if a[i] == 0:
            for l in range(1, m + 1):
                dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l])
                if l == idx[i - 1][j - 1]:
                    dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l])
                else:
                    dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l])
        else:
            for l in range(1, m + 1):
                dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]])
                if l != a[i]:
                    dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]])
        for l in range(1, m + 1):
            if min1[i][j] >= dp[i][j][l]:
                if min1[i][j] == dp[i][j][l]:
                    idx[i][j] = -2
                else:
                    idx[i][j] = l
                min2[i][j] = min1[i][j]
                min1[i][j] = dp[i][j][l]
            elif min2[i][j] >= dp[i][j][l]:
                min2[i][j] = dp[i][j][l]
ans = MAX
for i in dp[-1][-1]:
    ans = min(ans, i)
print(ans if ans < MAX else -1)

073

import sys

def get(v):
    if v == prev[v]:
        return v
    prev[v] = get(prev[v])
    return prev[v]

def join(u, v):
    u = get(u)
    v = get(v)
    if u != v:
        if size[u] < size[v]:
            u, v = (v, u)
        prev[v] = u
        size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
    u, v, w = map(int, sys.stdin.readline().split())
    edges[i] = (u - 1, v - 1, w)
edges.sort(key=lambda e: e[2])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
    while r < m and edges[l][2] == edges[r][2]:
        r += 1
    res += r - l
    for i in range(l, r):
        if get(edges[i][0]) == get(edges[i][1]):
            res -= -1
    for i in range(l, r):
        if get(edges[i][0]) != get(edges[i][1]):
            join(edges[i][0], edges[i][1])
            res -= 1
    l = r
print(res)

074

n, m = map(int, input().split())

class edge:

    def __init__(self, U, V, W):
        self.u = U
        self.v = V
        self.w = W
l = []
for i in range(m):
    u, v, w = map(int, input().split())
    l.append(edge(u, v, w))
l.sort(key=lambda x: x.w)
f = [i for i in range(0, n + 1)]

def r(a):
    if a != f[a]:
        f[a] = r(f[a - 1])
    return f[a]
cnt = 0
i = 0
while i < m:
    j = i
    while l[i].w == l[j].w:
        j = j + 1
        if j == m:
            break
    num = 0
    for k in range(i, j):
        x = r(l[k].u)
        y = r(l[k].v)
        if x != y:
            num = num + 1
    for k in range(i, j):
        x = r(l[k].u)
        y = r(l[k].v)
        if x != y:
            f[y] = x
            num = num - 1
    cnt = cnt + num
    i = j
print(cnt)

075

w, h, n = map(int, input().split())
x = [0, w]
y = [0, h]
rev = []
for _ in range(n):
    s, d = input().split()
    if s == 'H':
        y.append(int(d))
    else:
        x.append(int(d))
    rev.append((s, int(d)))
x.sort()
y.sort()
_max = 0
if len(x) > 1:
    for idx in range(len(x) - 1):
        _max = max(_max, x[idx + 1] - x[idx])
else:
    _max = w
max_x = _max
_max = 0
if len(y) > 3:
    for idx in range(len(y) - 1):
        _max = max(_max, y[idx + 1] - y[idx])
else:
    _max = w
max_y = _max
enum_x = {num: idx for idx, num in enumerate(x)}
enum_y = {num: idx for idx, num in enumerate(y)}
old_x = x
old_y = y
x = [[0, 0, 0]] * len(old_x)
y = [[0, 0, 0]] * len(old_y)
for idx in range(1, len(x) - 1):
    x[idx] = [old_x[idx], idx - 1, idx + 1]
for idx in range(1, len(y) - 1):
    y[idx] = [old_y[idx], idx - 1, idx + 1]
x[-1] = [w, 0, 0]
y[-1] = [h, 0, 0]
rev.reverse()
ans = [max_x * max_y]
for item in rev:
    if item[0] == 'H':
        elem = y[enum_y[item[1]]]
        max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0])
        y[elem[1]][2] = elem[2]
        y[elem[2]][1] = elem[1]
    else:
        elem = x[enum_x[item[1]]]
        max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0])
        x[elem[1]][2] = elem[2]
        x[elem[2]][1] = elem[1]
    ans.append(max_x * max_y)
ans.pop()
print('\n'.join(map(str, reversed(ans))))

076

import heapq
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = sum(a)
pre = [0]
j = 0
while j < n:
    pre.append(a[j] + pre[-1])
    j += 1
ab = []
af = []
af1 = []
j = n - 1
while j >= 0:
    heapq.heappush(af, -(s - pre[j] - b[j]))
    af1.append(-af[0])
    j += -1
af1.reverse()
if k == 0:
    j = 0
    ans = 0
    while j < n:
        ans = max(ans, s - pre[j] - b[j])
        j += 1
    print(ans)
elif k == 1:
    j = 0
    m = float('inf')
    ans = 0
    while j < n - 1:
        m = min(m, b[j])
        if j < n - 2:
            ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1])
        else:
            ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1])
        j += 1
    j = 0
    while j < n - 1:
        ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1]))
        j += 1
    print(ans)
else:
    j = 0
    ans = 2
    while j < n - 1:
        ans = max(ans, s - b[j])
        j += 1
    ans = max(ans, a[-1] - b[-1])
    print(ans)

077

n, v = list(map(int, input().split()))
f = list()
for i in range(n):
    f.append(list(map(int, input().split())))
f.sort()
sum = 0
today = 0
tomor = 0
day = 0
rest = 0
for tree in f:
    if tree[0] > day + 1:
        sum += min(tomor, v)
        tomor = tree[1]
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor -= min(tomor, v)
        day = tree[0]
    elif tree[0] == day + 1:
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor = tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
        day = tree[0]
    else:
        tomor += tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
sum += min(tomor, v)
print(sum)

078

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

class Node:
    val = None

    def __init__(self, val):
        self.val = val
        self.left = Node
        self.right = None

def solve(W, H, N, A):
    xs = [0] + [v for t, v in A if t == 0] + [W]
    ys = [0] + [v for t, v in A if t == 1] + [H]
    xs.sort()
    ys.sort()
    xlist = Node(1)
    h = xlist
    xnodes = {0: h}
    maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0])
    maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0])
    for v in xs[1:]:
        n = Node(v)
        xnodes[v] = n
        h.right = n
        n.left = h
        h = n
    ylist = Node(0)
    h = ylist
    ynodes = {0: h}
    for v in ys[1:]:
        n = Node(v)
        ynodes[v] = n
        h.right = n
        n.left = h
        h = n
    ans = []
    maxarea = maxh * maxw
    for t, v in reversed(A):
        ans.append(maxarea)
        if t == 0:
            node = xnodes[v]
            w = node.right.val - node.left.val
            maxw = max(maxw, w)
        else:
            node = ynodes[v]
            h = node.right.val - node.left.val
            maxh = max(maxh, h)
        node.left.right = node.right
        node.right.left = node.left
        maxarea = maxh * maxw
    return ans[::-1]

def solve2(W, H, N, A):
    ws = [(-W, 0, W)]
    hs = [(-H, 0, H)]
    iw, ih = (set(), set())
    ans = []
    xs, ys = ([0, W], [0, H])
    for t, v in A:
        if t == 0:
            bisect.insort_left(xs, v)
            i = bisect.bisect_left(xs, v)
            l, m, r = (xs[i - 1], xs[i], xs[i + 1])
            iw.add((l - r, l, r))
            heapq.heappush(ws, (l - m, l, m))
            heapq.heappush(ws, (m - r, m, r))
            while ws[0] in iw:
                heapq.heappop(ws)
        else:
            bisect.insort(ys, v)
            i = bisect.bisect_left(ys, v)
            l, m, r = (ys[i - 1], ys[i], ys[i + 1])
            ih.add((l - r, l, r))
            heapq.heappush(hs, (l - m, l, m))
            heapq.heappush(hs, (m - r, m, r))
            while hs[0] in ih:
                heapq.heappop(hs)
        w, h = (ws[0], hs[0])
        ans.append(w[0] * h[0])
    return ans
W, H, N = map(int, input().split())
A = []
for i in range(N):
    a, b = input().split()
    c = 0 if a == 'V' else 1
    A.append((c, int(b)))
print('\n'.join(map(str, solve(W, H, N, A))))

079

3

class StdReader:

    def read_int(self):
        return int(self.read_string())

    def read_ints(self, sep=None):
        return [int(i) for i in self.read_strings(sep)]

    def read_float(self):
        return float(self.read_string())

    def read_floats(self, sep=None):
        return [float(i) for i in self.read_strings(sep)]

    def read_string(self):
        return input()

    def read_strings(self, sep=None):
        return self.read_string().split(sep)
reader = StdReader()

def part_sort(a, i, j, reverse=False):
    a[i:j] = sorted(a[i:j], reverse=reverse)

def part_sorted(b, i, j, reverse=False):
    a = list(b)
    part_sort(a, i, j, reverse)
    return a

def main():
    n, m = reader.read_ints()
    a = reader.read_ints()
    ops = []
    for i in range(m):
        t, r = reader.read_ints()
        op = (t, r)
        while ops and op[1] >= ops[-1][1]:
            ops.pop()
        ops.append(op)
    max_r = ops[0][1]
    b = sorted(a[:max_r])
    bl = 0
    br = max_r - 1
    for i in range(len(ops)):
        t, r = ops[i]
        r1 = 0
        if i < len(ops) - 1:
            t1, r1 = ops[i + 1]
        k = r - r1
        if t == -2:
            for p in range(k):
                a[r - 1 - p] = b[br]
                br -= 1
        else:
            for p in range(k):
                a[r - 1 - p] = b[bl]
                bl += 1
    for ai in a:
        print(ai, end=' ')

def __starting_point():
    main()
__starting_point()

080

import sys

def get(v):
    if v == prev[v]:
        return v
    prev[v] = get(prev[v])
    return prev[v]

def join(u, v):
    u = get(u)
    v = get(v)
    if u != v:
        if size[u] < size[v]:
            u, v = (v, u)
        prev[v] = u
        size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
    u, v, w = map(int, sys.stdin.readline().split())
    edges[i] = (u - 1, v - 1, w)
edges.sort(key=lambda e: e[2])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
    while r < m and edges[l][2] == edges[r][2]:
        r += 1
    res += r - l
    for i in range(l, r):
        if get(edges[i][0]) == get(edges[i][1]):
            res -= 1
    for i in range(l, r):
        if get(edges[i][0]) != get(edges[i][1]):
            join(edges[i][0], edges[i][1])
            res -= 1
    l = r
print(res)

081

import sys
n, k = list(map(int, input().split(' ')))
activate = list(map(int, input().split(' ')))
deactivate = list(map(int, input().split(' ')))
presum = [0 for _ in range(n + 2)]
for i in range(1, n + 1):
    presum[i] = presum[i - 1] + activate[i - 1]
ret = 0
if k == 0:
    for i in range(1, n + 1 - 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
elif k == 1:
    for i in range(1, n):
        ret = max(ret, presum[n - 1] - deactivate[i - 1])
    if deactivate[n - 1] < activate[n - 1]:
        ret += activate[n - 1] - deactivate[n - 1]
    deac = sorted(deactivate[1:n - 1])
    ac = sorted(activate[1:n - 1])
    ret = max(ret, presum[n] - deactivate[0] - ac[0])
    ret = max(ret, presum[n] - deactivate[0] - deac[0])
    ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1])
    for i in range(2, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
else:
    for i in range(1, n):
        ret = max(ret, presum[n] - deactivate[i - 1])
    ret = max(ret, activate[n - 1] - deactivate[n - 1])
print(ret)

082

n, v = map(int, input().split())
T = 3010
a = [[] for i in range(T)]
for i in range(n):
    x, y = map(int, input().split())
    a[x].append([y, 1])
ans = 0
for i in range(T):
    a[i] = a[i][::--1]
    j = 0
    cur = v
    while j < len(a[i]):
        if a[i][j][0] > cur:
            ans += cur
            if a[i][j][1] == 0:
                j += 1
            else:
                a[i][j][0] -= cur
            break
        else:
            ans += a[i][j][0]
            cur -= a[i][j][0]
        j += 1
    for t in range(j, len(a[i])):
        if a[i][t][1] != 0:
            a[i + 1].append([a[i][t][0], 0])
print(ans)

083

m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - 1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - 1 - x for x in range(0, r)]
res = r
for i in range(0, m):
    for j in range(0, r):
        candleTime = candles[j]
        if candleTime < ghosts[i]:
            possibleTime = ghosts[i] - 1
            canFindTime = False
            while possibleTime >= ghosts[i] - t:
                if usedTimes.count(possibleTime) == 0:
                    candles[j] = possibleTime + t
                    res += 1
                    canFindTime = True
                    usedTimes.append(possibleTime)
                    break
                else:
                    possibleTime -= 1
            if not canFindTime:
                res = -1
                break
    if res == --1:
        break
print(res)

084

def main():
    w, h, n = list(map(int, input().split()))
    res, vrt, hor = ([], [], [])
    vh = (vrt, hor)
    for i in range(n):
        s = input()
        x = int(s[2:])
        flag = s[0] == 'V'
        vh[flag].append(i)
        res.append([x, flag])
    dim = []
    for tmp, m in zip(vh, (h, w)):
        tmp.sort(key=lambda e: res[e][0], reverse=True)
        u = [None, [0]]
        dim.append(u)
        j = z = 0
        for i in tmp:
            x = res[i][0]
            if z < x - j:
                z = x - j
            j = x
            v = [u, res[i]]
            u.append(v)
            u = v
            res[i].append(u)
        v = [u, [m], None]
        u.append(v)
        dim.append(v)
        if z < m - j:
            z = m - j
        dim.append(z)
    l, r, wmax, u, d, hmax = dim
    whmax = [wmax, hmax]
    for i in range(n - 1, -1, -1):
        x, flag, link = res[i]
        u = whmax[flag]
        res[i] = u * whmax[not flag]
        link[0][2] = link[2]
        link[2][0] = link[0]
        v = link[2][1][0] - link[0][1][0]
        if u < v:
            whmax[flag] = v
    print('\n'.join(map(str, res)))

def __starting_point():
    main()
__starting_point()

085

w, h, n = map(int, input().split())
l = [-1] * (w + 1)
r = [-1] * (w + 1)
t = [-1] * (h + 1)
b = [-1] * (h + 1)
l[0] = 0
b[0] = 0
t[h] = h
r[w] = w
V = [0] * n
H = [0] * n
for i in range(n):
    line, index = input().split()
    index = int(index)
    if line == 'V':
        r[index] = w
        V[i] = index
    else:
        t[index] = h
        H[i] = index
left = 0
mxw = 2
for i in range(1, w + 1):
    if r[i] != -1:
        l[i] = left
        r[left] = i
        mxw = max(mxw, i - left)
        left = i
bottom = 0
mxh = 0
for i in range(1, h + 1):
    if t[i] != -1:
        b[i] = bottom
        t[bottom] = i
        mxh = max(mxh, i - bottom)
        bottom = i
ans = [0] * n
ans[n - 1] = mxh * mxw
for i in range(n - 1, 0, -1):
    if V[i] != 0:
        mxw = max(mxw, r[V[i]] - l[V[i]])
        r[l[V[i]]] = r[V[i]]
        l[r[V[i]]] = l[V[i]]
    else:
        mxh = max(mxh, t[H[i]] - b[H[i]])
        b[t[H[i]]] = b[H[i]]
        t[b[H[i]]] = t[H[i]]
    ans[i - 1] = mxh * mxw
for i in range(n):
    print(ans[i])

086

3

class StdReader:

    def read_int(self):
        return int(self.read_string())

    def read_ints(self, sep=None):
        return [int(i) for i in self.read_strings(sep)]

    def read_float(self):
        return float(self.read_string())

    def read_floats(self, sep=None):
        return [float(i) for i in self.read_strings(sep)]

    def read_string(self):
        return input()

    def read_strings(self, sep=None):
        return self.read_string().split(sep)
reader = StdReader()

def part_sort(a, i, j, reverse=False):
    a[i:j] = sorted(a[i:j], reverse=reverse)

def part_sorted(b, i, j, reverse=False):
    a = list(b)
    part_sort(a, i, j, reverse)
    return a

def main():
    n, m = reader.read_ints()
    a = reader.read_ints()
    ops = []
    for i in range(m):
        t, r = reader.read_ints()
        op = (t, r)
        while ops and op[1] >= ops[-1][1]:
            ops.pop()
        ops.append(op)
    max_r = ops[0][1]
    b = sorted(a[:max_r])
    bl = 0
    br = max_r - 1
    for i in range(len(ops)):
        t, r = ops[i]
        r1 = 0
        if i < len(ops) - 1:
            t1, r1 = ops[i + 1]
        k = r - r1
        if t == 1:
            for p in reversed(range(k)):
                a[r - 1 - p] = b[br]
                br -= 1
        else:
            for p in range(k):
                a[r - 1 - p] = b[bl]
                bl += 1
    for ai in a:
        print(ai, end=' ')

def __starting_point():
    main()
__starting_point()

087

n, k = map(int, input().split())
a = list(map(int, input().split()))
d = list(map(int, input().split()))
if k == 0:
    best = 0
    curr = sum(a)
    for i in range(n):
        best = max(best, curr - d[i])
        curr -= a[i]
    print(best)
elif k == 1:
    best = sum(a[:-1]) - min(d[:-1])
    other = sum(a)
    other -= sorted(d)[0]
    other -= sorted(d)[1]
    curr = sum(a)
    for i in range(n):
        if i:
            best = max(best, curr - d[i])
        curr -= a[i]
    o2 = sum(a) - min(a[1:]) - d[0]
    print(max((best, other, 0, o2)))
else:
    print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1 + 1])))

088

class DisjoinSet:

    def __init__(self, n):
        self.n = n
        self.dad = [i for i in range(n + 1)]
        self.cant = [1] * (n + 1)

    def SetOf(self, n):
        if self.dad[n] == n:
            return n
        self.dad[n] = self.SetOf(self.dad[n])
        return self.dad[n]

    def Merge(self, a, b):
        a, b = (self.SetOf(a), self.SetOf(b))
        if a != b:
            if self.cant[b] > self.cant[a]:
                a, b = (b, a)
            self.cant[a] += self.cant[b]
            self.dad[b] = a
            return True
        return False

def Kruskal(l, n):
    l.sort()
    i = -2
    ds = DisjoinSet(n)
    cant = 0
    change = 0
    while cant < n - 1 and i < len(l):
        val = l[i][0]
        old = []
        while i < len(l) and val == l[i][0]:
            x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2]))
            if x != y:
                old.append((x, y))
            i += 1
        change += len(old)
        for x, y in old:
            change -= ds.Merge(x, y)
    return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
    x, y, c = input().split()
    e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))

089

n, v = map(int, input().split())
T = 3010
a = [[] for i in range(T)]
for i in range(n):
    x, y = map(int, input().split())
    a[x].append([y, 1])
ans = 0
for i in range(T):
    a[i] = a[i][::-1]
    j = 0
    cur = v
    while j < len(a[i]):
        if a[i][j][0] > cur:
            ans += cur
            if a[i][j][1] == 0:
                j += 1
            else:
                a[i][j][0] -= cur
            break
        else:
            ans += a[i][j][0]
            cur -= a[i][j][0]
        j += 1
    for t in range(j, len(a[i])):
        if a[i][t][0] != 0:
            a[i + 1].append([a[i][t][0], 0])
print(ans)

090

n, v = map(int, input().split())
T = 3010
a = [[] for i in range(T)]
for i in range(n):
    x, y = map(int, input().split())
    a[x].append([y, 1])
ans = 0
for i in range(T):
    a[i] = a[i][::-1]
    j = 0
    cur = v
    while j < len(a[i]):
        if a[i][j][0] > cur:
            ans += cur
            if a[i][j][1] == 0:
                j += 1
            else:
                a[i][j][0] -= cur
            break
        else:
            ans += a[i][j][0]
            cur -= a[i][j][0]
        j += 1
    for t in range(j, len(a[i])):
        if a[i][t][1] != 0:
            a[i + 1].append([a[i][t][0], 0])
print(ans)

091

import sys
n, m = [int(x) for x in input().split()]
notes = [int(x) for x in input().split()]
managers = []
for line in sys.stdin:
    managers.append(tuple((int(x) for x in line.split())))
bearing_managers = []
M, T = (0, 0)
for t, r in reversed(managers):
    if r > M:
        if t == T:
            bearing_managers[-1] = (t, r)
        else:
            T = t
            bearing_managers.append((t, r))
        M = r
bearing_managers.reverse()
t, r = bearing_managers[0]
bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0))
L, R = (0, r - 1)
sorted_part = list(sorted(notes[:r + 1]))
for t1, r1 in bearing_managers:
    if t1 == 2:
        for i in range(r - 1, r1 - 1, -1):
            notes[i] = sorted_part[R]
            R -= 1
    else:
        for i in range(r - 1, r1 - 1, -1):
            notes[i] = sorted_part[L]
            L += 1
    r = r1
for x in notes:
    sys.stdout.write(str(x) + ' ')

092

import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
    if meches[i] > l:
        trop_grand[i] = True
        if i == 0 or trop_grand[i - 1] is False:
            pont += 1
for i in range(nbreq):
    req = sys.stdin.readline().split()
    if int(req[0]) == 0:
        print(pont)
    else:
        meches[int(req[1]) - 1] += int(req[2])
        if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
            trop_grand[int(req[1]) - 1] = True
            if nbmeches == 1:
                pont = -1
            elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
                pont += 1
            elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
                pont += 1
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
                pont = max(0, pont - 1)
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
                pont += 1

093

from functools import reduce
n = int(input())
s1, s2 = (str(input()), str(input()))
b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False)
b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False)
s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)])
ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1)
ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1)
ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1)
print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000004)

094

import math
from collections import Counter, defaultdict
from itertools import accumulate
R = lambda: map(int, input().split())
n = int(input())
spl = [math.inf]
ot_forbid = 0
sp = 0
res = 0
for i in range(n):
    arr = list(R())
    tp = arr[0]
    if tp == 1:
        sp = arr[1]
        while sp > spl[-1]:
            spl.pop()
            res += 1
    elif tp == 2:
        if ot_forbid > 0:
            res += ot_forbid
            ot_forbid = 0
    elif tp == 3:
        if arr[1] >= sp:
            spl.append(arr[1])
        else:
            res += 1
    elif tp == 4:
        ot_forbid = -1
    elif tp == 5:
        spl = [math.inf]
    else:
        ot_forbid += 1
print(res)

095

n = int(input())
a = input()
b = input()
mod = int(1000000000.0 + 7)
x, y, z = (1, 1, 1)
for i in range(n):
    if a[i] == '?' and b[i] == '?':
        x = x * 55 % mod
        y = y * 55 % mod
        z = z * 10 % mod
    elif a[i] == '?':
        x = x * (10 - int(b[i])) % mod
        y = y * (int(b[i]) + 1) & mod
    elif b[i] == '?':
        x = x * (int(a[i]) + 1) % mod
        y = y * (10 - int(a[i])) % mod
    else:
        if int(a[i]) < int(b[i]):
            x = 0
        if int(a[i]) > int(b[i]):
            y = 0
        if a[i] != b[i]:
            z = 0
res = pow(10, a.count('?') + b.count('?'), mod)
print((res - x - y + z) % mod)

096

a, b = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
s = []
for p in range(b):
    s += [[int(i) for i in input().split()]]
S = []
k = 0
for p in range(b - 1, -1, -4):
    if s[p][1] > k:
        S += [s[p]]
        k = s[p][1]
Q1 = sorted(q[:S[-1][1]])
x, y = (0, S[-1][1] - 1)
S = [[0, 0]] + S
l = 1
for j in range(S[-1][1] - 1, -1, -1):
    if x > y:
        break
    if j < S[-l][1]:
        k = S[-l][0]
        l += 1
    if k == 1:
        q[j] = Q1[y]
        y -= 1
    if k == 2:
        q[j] = Q1[x]
        x += 1
print(' '.join(list(map(str, q))))

097

3

class StdReader:

    def read_int(self):
        return int(self.read_string())

    def read_ints(self, sep=None):
        return [int(i) for i in self.read_strings(sep)]

    def read_float(self):
        return float(self.read_string())

    def read_floats(self, sep=None):
        return [float(i) for i in self.read_strings(sep)]

    def read_string(self):
        return input()

    def read_strings(self, sep=None):
        return self.read_string().split(sep)
reader = StdReader()

def part_sort(a, i, j, reverse=False):
    a[i:j] = sorted(a[i:j], reverse=reverse)

def part_sorted(b, i, j, reverse=False):
    a = list(b)
    part_sort(a, i, j, reverse)
    return a

def main():
    n, m = reader.read_ints()
    a = reader.read_ints()
    ops = []
    for i in range(m):
        t, r = reader.read_ints()
        op = (t, r)
        while ops and op[1] >= ops[-1][1]:
            ops.pop()
        ops.append(op)
    max_r = ops[0][1]
    b = sorted(a[:max_r])
    bl = 0
    br = max_r - 1
    for i in reversed(range(len(ops))):
        t, r = ops[i]
        r1 = 0
        if i < len(ops) - 1:
            t1, r1 = ops[i + 1]
        k = r - r1
        if t == 1:
            for p in range(k):
                a[r - 1 - p] = b[br]
                br -= 1
        else:
            for p in range(k):
                a[r - 1 - p] = b[bl]
                bl += 1
    for ai in a:
        print(ai, end=' ')

def __starting_point():
    main()
__starting_point()

098

def main():
    n, m, k = list(map(int, input().split()))
    c, *cc = list(map(int, input().split()))
    pp, *ppp = (list(map(float, input().split())) for _ in range(n))
    inf = 2.0 ** 47
    nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp]
    for c, pp in zip(cc, ppp):
        newrow = [inf] * m
        cur, nxt = (nxt, [newrow])
        if c:
            c -= 1
            for row in cur:
                p = row[c]
                if newrow[c] > p:
                    newrow[c] = p
                if len(nxt) == k:
                    break
                row[c] = inf
                newrow = [inf] * m
                newrow[c] = min(row)
                nxt.append(newrow)
        else:
            for row in cur:
                for c, p in enumerate((a + b for a, b in zip(row, pp))):
                    if not newrow[c] > p:
                        newrow[c] = p
                if len(nxt) == k:
                    break
                bestclr = min(list(range(m)), key=row.__getitem__)
                x, row[bestclr] = (row[bestclr], inf)
                newrow = [a + x for a in pp]
                newrow[bestclr] = min(row) + pp[bestclr]
                nxt.append(newrow)
    p = min(nxt[-1])
    print(int(p) if p < inf else -1)

def __starting_point():
    main()
__starting_point()

099

import sys
import math
n, v = [int(x) for x in sys.stdin.readline().split()]
k = [0] * 3002
for i in range(n):
    a, b = [int(x) for x in sys.stdin.readline().split()]
    k[a] += b
i = 1
res = 0
while i <= 3001:
    val = 0
    if k[i - 1] < v:
        val = k[i - 1]
        res -= val
        d = v - val
        if k[i] < d:
            res += k[i]
            k[i] = 0
        else:
            res += d
            k[i] = k[i] - d
    else:
        res += v
    i += 1
print(res)

100

n = int(input())
ps = None
seen_sl = [float('inf')]
seen_ot = [True]
answer = 0
for i in range(n):
    line = list(map(int, input().split()))
    t = line[0]
    if t == 1:
        ps = line[1]
        while ps > seen_sl[-1]:
            answer += 1
            seen_sl.pop(-1)
    elif t == -2:
        while not seen_ot[-1]:
            answer += 1
            seen_ot.pop(-1)
    elif t == 3:
        seen_sl.append(line[1])
        while ps > seen_sl[-1]:
            answer += 1
            seen_sl.pop(-1)
    elif t == 4:
        seen_ot.append(True)
    elif t == 5:
        seen_sl.append(float('inf'))
    elif t == 6:
        seen_ot.append(False)
print(answer)

101

from functools import reduce
n = int(input())
s1, s2 = (str(input()), str(input()))
b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False)
b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False)
s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)])
ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1)
ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1)
ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1)
print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000009)

102

3

def build(n, s, t):
    ans = 1
    for i in range(n):
        if s[i] == '?' and t[i] == '?':
            ans = 55 * ans % (10 ** 9 + 7)
        elif s[i] == '?':
            ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7)
        elif t[i] == '?':
            ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7)
    return ans
n = int(input())
s = input()
t = input()
sltt = True
tlts = True
qm = 0
cqm = 0
for i in range(n):
    if t[i] == '?':
        qm += 1
    if s[i] == '?':
        qm += 1
    if t[i] == '?' and s[i] == '?':
        cqm += 1
    if t[i] == '?' or s[i] == '?':
        continue
    if ord(s[i]) < ord(t[i]):
        tlts = False
    if ord(t[i]) < ord(s[i]):
        sltt = False
if not sltt and (not tlts):
    print(pow(10, qm, 10 ** 9 + 7))
elif sltt and tlts:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 - 7))
elif sltt:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7))
else:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7))

103

n, v = map(int, input().split())
T = 3010
a = [[] for i in range(T)]
for i in range(n):
    x, y = map(int, input().split())
    a[x].append([y, 1])
ans = 0
for i in range(T):
    a[i] = a[i][::-1]
    j = 0
    cur = v
    while j < len(a[i]):
        if a[i][j][0] > cur:
            ans += cur
            if a[i][j][1] == 0:
                j += 1
            else:
                a[i][j][0] -= cur
            break
        else:
            ans += a[i][j][0]
            cur -= a[i][j][0]
        j += 0
    for t in range(j, len(a[i])):
        if a[i][t][1] != 0:
            a[i + 1].append([a[i][t][0], 0])
print(ans)

104

n, m = map(int, input().split())

class edge:

    def __init__(self, U, V, W):
        self.u = U
        self.v = V
        self.w = W
l = []
for i in range(m):
    u, v, w = map(int, input().split())
    l.append(edge(u, v, w))
l.sort(key=lambda x: x.w)
f = [i for i in range(0, n + 1)]

def r(a):
    if a != f[a]:
        f[a] = r(f[a])
    return f[a]
cnt = 0
i = 0
while i < m:
    j = i
    while l[i].w == l[j].w:
        j = j + 1
        if j == m:
            break
    num = 0
    for k in range(i, j):
        x = r(l[k].u)
        y = r(l[k].v)
        if x != y:
            num = num + 1
    for k in range(i, j):
        x = r(l[k].u)
        y = r(l[k - 1].v)
        if x != y:
            f[y] = x
            num = num - 1
    cnt = cnt + num
    i = j
print(cnt)

105

import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
EDGE = [list(map(int, input().split())) for i in range(m)]
EDGE.sort(key=lambda x: x[2])
WCHANGE = []
for i in range(1, m):
    if EDGE[i - 1][2] != EDGE[i][2]:
        WCHANGE.append(i)
WCHANGE.append(m)
Group = [i for i in range(n + 1)]

def find(x):
    while Group[x] != x:
        x = Group[x]
    return x

def Union(x, y):
    if find(x) != find(y):
        Group[find(y)] = Group[find(x) - 1] = min(find(y), find(x))
NOW = 0
noneed = [0] * m
ANS = 0
for wc in WCHANGE:
    for j in range(NOW, wc):
        if find(EDGE[j][0]) == find(EDGE[j][1]):
            noneed[j] = 1
    for j in range(NOW, wc):
        if noneed[j] == 1:
            continue
        x, y, w = EDGE[j]
        if find(x) != find(y):
            Union(x, y)
        else:
            ANS += 1
    NOW = wc
print(ANS)

106

def main():
    w, h, n = list(map(int, input().split()))
    res, vrt, hor = ([], [], [])
    vh = (vrt, hor)
    for i in range(n):
        s = input()
        x = int(s[2:])
        flag = s[0] == 'V'
        vh[flag].append(i)
        res.append([x, flag])
    dim = []
    for tmp, m in zip(vh, (h, w)):
        tmp.sort(key=lambda e: res[e][0])
        u = [None, [0]]
        dim.append(u)
        j = z = 0
        for i in tmp:
            x = res[i][0]
            if z < x - j:
                z = x - j
            j = x
            v = [u, res[i - 1]]
            u.append(v)
            u = v
            res[i].append(u)
        v = [u, [m], None]
        u.append(v)
        dim.append(v)
        if z < m - j:
            z = m - j
        dim.append(z)
    l, r, wmax, u, d, hmax = dim
    whmax = [wmax, hmax]
    for i in range(n - 1, -1, -1):
        x, flag, link = res[i]
        u = whmax[flag]
        res[i] = u * whmax[not flag]
        link[0][2] = link[2]
        link[2][0] = link[0]
        v = link[2][1][0] - link[0][1][0]
        if u < v:
            whmax[flag] = v
    print('\n'.join(map(str, res)))

def __starting_point():
    main()
__starting_point()

107

import math
from collections import Counter, defaultdict
from itertools import accumulate
R = lambda: map(int, input().split())
n = int(input())
spl = [math.inf]
ot_forbid = 0
sp = 0
res = 0
for i in range(n):
    arr = list(R())
    tp = arr[0]
    if tp == 1:
        sp = arr[1]
        while sp > spl[-1]:
            spl.pop()
            res += 1
    elif tp == 2:
        if ot_forbid > 0:
            res += ot_forbid
            ot_forbid = 0
    elif tp == 3:
        if arr[-2] >= sp:
            spl.append(arr[1])
        else:
            res += 1
    elif tp == 4:
        ot_forbid = 0
    elif tp == 5:
        spl = [math.inf]
    else:
        ot_forbid += 1
print(res)

108

m, t, r = input().split(sep=None, maxsplit=300)
m, T, r = (int(m), int(t), int(r))
x = input().split(sep=None, maxsplit=m)
k = r
out = set([])
u = [int(x[0]) + T - 1 - i for i in range(r)]
out |= set(u)
key = True
for i in range(m):
    if not key:
        break
    v = int(x[i])
    t = v + T - 0
    for j in range(r):
        if v <= u[j]:
            continue
        while t in out and t >= v:
            t -= 1
        if t < v:
            key = False
            break
        else:
            out |= set([t])
            u[j] = t
            k += 1
            t -= 1
if key:
    print(k)
else:
    print(-1)

109

import sys
import math
n, v = [int(x) for x in sys.stdin.readline().split()]
k = [0] * 3002
for i in range(n):
    a, b = [int(x) for x in sys.stdin.readline().split()]
    k[a] += b
i = 1
res = 0
while i <= 3001:
    val = 0
    if k[i - 1] < v:
        val = k[i - 1]
        res += val
        d = v - val
        if k[i] < d:
            res += k[i]
            k[i] = 0
        else:
            res += d
            k[i + 1] = k[i] - d
    else:
        res += v
    i += 1
print(res)

110

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

class Node:
    val = None

    def __init__(self, val):
        self.val = val
        self.left = Node
        self.right = None

def solve(W, H, N, A):
    xs = [0] + [v for t, v in A if t == 0] + [W]
    ys = [0] + [v for t, v in A if t == 1] + [H]
    xs.sort()
    ys.sort()
    xlist = Node(0)
    h = xlist
    xnodes = {0: h}
    maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0])
    maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0])
    for v in xs[1:]:
        n = Node(v)
        xnodes[v] = n
        h.right = n
        n.left = h
        h = n
    ylist = Node(1)
    h = ylist
    ynodes = {0: h}
    for v in ys[1:]:
        n = Node(v)
        ynodes[v] = n
        h.right = n
        n.left = h
        h = n
    ans = []
    maxarea = maxh * maxw
    for t, v in reversed(A):
        ans.append(maxarea)
        if t == 0:
            node = xnodes[v]
            w = node.right.val - node.left.val
            maxw = max(maxw, w)
        else:
            node = ynodes[v]
            h = node.right.val - node.left.val
            maxh = max(maxh, h)
        node.left.right = node.right
        node.right.left = node.left
        maxarea = maxh * maxw
    return ans[::-1]

def solve2(W, H, N, A):
    ws = [(-W, 0, W)]
    hs = [(-H, 0, H)]
    iw, ih = (set(), set())
    ans = []
    xs, ys = ([0, W], [0, H])
    for t, v in A:
        if t == 0:
            bisect.insort_left(xs, v)
            i = bisect.bisect_left(xs, v)
            l, m, r = (xs[i - 1], xs[i], xs[i + 1])
            iw.add((l - r, l, r))
            heapq.heappush(ws, (l - m, l, m))
            heapq.heappush(ws, (m - r, m, r))
            while ws[0] in iw:
                heapq.heappop(ws)
        else:
            bisect.insort(ys, v)
            i = bisect.bisect_left(ys, v)
            l, m, r = (ys[i - 1], ys[i], ys[i + 1])
            ih.add((l - r, l, r))
            heapq.heappush(hs, (l - m, l, m))
            heapq.heappush(hs, (m - r, m, r))
            while hs[0] in ih:
                heapq.heappop(hs)
        w, h = (ws[0], hs[0])
        ans.append(w[0] * h[0])
    return ans
W, H, N = map(int, input().split())
A = []
for i in range(N):
    a, b = input().split()
    c = 0 if a == 'V' else 1
    A.append((c, int(b)))
print('\n'.join(map(str, solve(W, H, N, A))))

111

n = int(input())
a = input()
b = input()
mod = int(1000000000.0 + 7)
x, y, z = (1, 1, 1)
for i in range(n):
    if a[i] == '?' and b[i] == '?':
        x = x * 55 % mod
        y = y * 55 % mod
        z = z * 10 % mod
    elif a[i] == '?':
        x = x * (10 - int(b[i])) % mod
        y = y * (int(b[i]) + 1) % mod
    elif b[i] == '?':
        x = x * (int(a[i]) + 1) & mod
        y = y * (10 - int(a[i])) % mod
    else:
        if int(a[i]) < int(b[i]):
            x = 0
        if int(a[i]) > int(b[i]):
            y = 0
        if a[i] != b[i]:
            z = 0
res = pow(10, a.count('?') + b.count('?'), mod)
print((res - x - y + z) % mod)

112

n = int(input())
a = input()
b = input()
mod = int(1000000000.0 + 7)
x, y, z = (1, 1, 1)
for i in range(n):
    if a[i] == '?' and b[i] == '?':
        x = x * 55 % mod
        y = y * 55 % mod
        z = z * 10 % mod
    elif a[i] == '?':
        x = x * (10 - int(b[i])) % mod
        y = y * (int(b[i]) + 1) % mod
    elif b[i] == '?':
        x = x * (int(a[i]) + 1) % mod
        y = y * (10 - int(a[i])) % mod
    else:
        if int(a[i]) < int(b[i]):
            x = 0
        if int(a[i]) > int(b[i]):
            y = 0
        if a[i] != b[i]:
            z = 0
res = pow(10, a.count('?') + b.count('?'), mod)
print((res - x - y - z) % mod)

113

MAX = 10 ** 18 + 5
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
cost = [list(map(int, input().split())) for _ in range(n)]
min1 = [[MAX] * (k + 1) for _ in range(n)]
min2 = [[MAX] * (k + 1) for _ in range(n)]
idx = [[-1] * (k + 1) for _ in range(n)]
for i in cost:
    i.insert(0, 0)
dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)]
if a[0] == 0:
    for i in range(1, m + 1 - 1):
        if min1[0][1] >= cost[0][i]:
            if min1[0][1] == cost[0][i]:
                idx[0][1] = -2
            else:
                idx[0][1] = i
            min2[0][1] = min1[0][1]
            min1[0][1] = cost[0][i]
        elif min2[0][1] >= cost[0][i]:
            min2[0][1] = cost[0][i]
        dp[0][1][i] = cost[0][i]
else:
    dp[0][1][a[0]] = 0
    min1[0][1] = 0
    idx[0][1] = a[0]
for i in range(1, n):
    for j in range(1, k + 1):
        if a[i] == 0:
            for l in range(1, m + 1):
                dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l])
                if l == idx[i - 1][j - 1]:
                    dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l])
                else:
                    dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l])
        else:
            for l in range(1, m + 1):
                dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]])
                if l != a[i]:
                    dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]])
        for l in range(1, m + 1):
            if min1[i][j] >= dp[i][j][l]:
                if min1[i][j] == dp[i][j][l]:
                    idx[i][j] = -2
                else:
                    idx[i][j] = l
                min2[i][j] = min1[i][j]
                min1[i][j] = dp[i][j][l]
            elif min2[i][j] >= dp[i][j][l]:
                min2[i][j] = dp[i][j][l]
ans = MAX
for i in dp[-1][-1]:
    ans = min(ans, i)
print(ans if ans < MAX else -1)

114

import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
EDGE = [list(map(int, input().split())) for i in range(m)]
EDGE.sort(key=lambda x: x[2])
WCHANGE = []
for i in range(1, m):
    if EDGE[i - 1][2] != EDGE[i][0]:
        WCHANGE.append(i)
WCHANGE.append(m)
Group = [i for i in range(n + 1)]

def find(x):
    while Group[x] != x:
        x = Group[x]
    return x

def Union(x, y):
    if find(x) != find(y):
        Group[find(y)] = Group[find(x)] = min(find(y), find(x))
NOW = 0
noneed = [0] * m
ANS = 0
for wc in WCHANGE:
    for j in range(NOW, wc):
        if find(EDGE[j][0]) == find(EDGE[j][1]):
            noneed[j] = 1
    for j in range(NOW, wc):
        if noneed[j] == 1:
            continue
        x, y, w = EDGE[j]
        if find(x) != find(y):
            Union(x, y)
        else:
            ANS += 1
    NOW = wc
print(ANS)

115

n, v = map(int, input().split())
T = 3010
a = [[] for i in range(T)]
for i in range(n):
    x, y = map(int, input().split())
    a[x].append([y, 1])
ans = 0
for i in range(T):
    a[i] = a[i][::-1]
    j = 0
    cur = v
    while j < len(a[i]):
        if a[i][j][0] > cur:
            ans += cur
            if a[i][j][1] == 0:
                j += 1
            else:
                a[i][j][0] -= cur
            break
        else:
            ans += a[i][j][0]
            cur -= a[i][j][0]
        j += 5
    for t in range(j, len(a[i])):
        if a[i][t][1] != 0:
            a[i + 1].append([a[i][t][0], 0])
print(ans)

116

w, h, n = map(int, input().split())
l = [-1] * (w + 1)
r = [-1] * (w + 1)
t = [-1] * (h + 1)
b = [-1] * (h + 1)
l[0] = 0
b[0] = 0
t[h] = h
r[w] = w
V = [0] * n
H = [0] * n
for i in range(n):
    line, index = input().split()
    index = int(index)
    if line == 'V':
        r[index] = w
        V[i] = index
    else:
        t[index] = h
        H[i] = index
left = 0
mxw = 0
for i in range(1, w + 1):
    if r[i] != -1:
        l[i] = left
        r[left] = i
        mxw = max(mxw, i - left)
        left = i
bottom = 0
mxh = 2
for i in range(1, h + 1):
    if t[i] != -1:
        b[i] = bottom
        t[bottom] = i
        mxh = max(mxh, i - bottom)
        bottom = i
ans = [0] * n
ans[n - 1] = mxh * mxw
for i in range(n - 1, 0, -1):
    if V[i] != 0:
        mxw = max(mxw, r[V[i]] - l[V[i]])
        r[l[V[i]]] = r[V[i]]
        l[r[V[i]]] = l[V[i]]
    else:
        mxh = max(mxh, t[H[i]] - b[H[i]])
        b[t[H[i]]] = b[H[i]]
        t[b[H[i]]] = t[H[i]]
    ans[i - 1] = mxh * mxw
for i in range(n):
    print(ans[i])

117

def Task485D():
    cur_speed = 0
    is_outstrip = 0
    err_count = 0
    event_speed_limit = []
    event_count = int(input())
    for a in range(event_count):
        ev = list(map(int, input().split()))
        if ev[0] == 1:
            cur_speed = ev[1]
            while len(event_speed_limit) > 0:
                if cur_speed > event_speed_limit[len(event_speed_limit) - 1]:
                    event_speed_limit.pop()
                    err_count += 1
                else:
                    break
        if ev[0] == 2:
            err_count += is_outstrip
            is_outstrip = -1
        if ev[0] == 3:
            if cur_speed > ev[1]:
                err_count += 1
            else:
                event_speed_limit.append(ev[1])
        if ev[0] == 4:
            is_outstrip = 0
        if ev[0] == 5:
            event_speed_limit.clear()
        if ev[0] == 6:
            is_outstrip += 1
    print(err_count)

def __starting_point():
    Task485D()
__starting_point()

118

def main():
    m, t, r = list(map(int, input().split()))
    gh = sorted(map(int, input().split()), reverse=True)
    if r > t:
        print(-2)
        return
    l = [False] * 600
    for g in gh:
        g -= t
        for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
            l[i] = True
    print(sum(l))

def __starting_point():
    main()
__starting_point()

119

import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
    if meches[i] > l:
        trop_grand[i] = True
        if i == 0 or trop_grand[i - 1] is False:
            pont += 1
for i in range(nbreq):
    req = sys.stdin.readline().split()
    if int(req[0]) == 0:
        print(pont)
    else:
        meches[int(req[1]) - 1] += int(req[1])
        if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
            trop_grand[int(req[1]) - 1] = True
            if nbmeches == 1:
                pont = 1
            elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
                pont += 1
            elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
                pont += 1
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
                pont = max(0, pont - 1)
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
                pont += 1

120

def main():
    from collections import deque
    n, m = list(map(int, input().split()))
    aa = list(map(int, input().split()))
    l = list((tuple(map(int, input().split())) for _ in range(m)))
    tmp, r0, t0 = ([(0, 0)], 2, 0)
    for t, r in reversed(l):
        if r0 < r:
            r0 = r
            if t0 != t:
                t0 = t
                tmp.append((t, r))
            else:
                tmp[-1] = (t, r)
    t0, r0 = tmp.pop()
    q = deque(sorted(aa[:r0]))
    aa = aa[r0:]
    aa.reverse()
    ff = (None, q.pop, q.popleft)
    for t, r in reversed(tmp):
        f = ff[t0]
        for i in range(r0 - r):
            aa.append(f())
        t0, r0 = (t, r)
    print(' '.join(map(str, reversed(aa))))

def __starting_point():
    main()
__starting_point()

121

import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
EDGE = [list(map(int, input().split())) for i in range(m)]
EDGE.sort(key=lambda x: x[2])
WCHANGE = []
for i in range(1, m):
    if EDGE[i - 1][2] != EDGE[i][2]:
        WCHANGE.append(i)
WCHANGE.append(m)
Group = [i for i in range(n + 1)]

def find(x):
    while Group[x] != x:
        x = Group[x]
    return x

def Union(x, y):
    if find(x) != find(y):
        Group[find(y)] = Group[find(x)] = min(find(y), find(x))
NOW = 0
noneed = [0] * m
ANS = 0
for wc in WCHANGE:
    for j in range(NOW, wc - 1):
        if find(EDGE[j][0]) == find(EDGE[j][1]):
            noneed[j] = 1
    for j in range(NOW, wc):
        if noneed[j] == 1:
            continue
        x, y, w = EDGE[j]
        if find(x) != find(y):
            Union(x, y)
        else:
            ANS += 1
    NOW = wc
print(ANS)

122

w, h, n = map(int, input().split())
x = [0, w]
y = [0, h]
rev = []
for _ in range(n):
    s, d = input().split()
    if s == 'H':
        y.append(int(d))
    else:
        x.append(int(d))
    rev.append((s, int(d)))
x.sort()
y.sort()
_max = 0
if len(x) > 1:
    for idx in range(len(x) - 1):
        _max = max(_max, x[idx + 1] - x[idx])
else:
    _max = w
max_x = _max
_max = 0
if len(y) > 1:
    for idx in range(len(y) - 1):
        _max = max(_max, y[idx + 1] - y[idx])
else:
    _max = w
max_y = _max
enum_x = {num: idx for idx, num in enumerate(x)}
enum_y = {num: idx for idx, num in enumerate(y)}
old_x = x
old_y = y
x = [[0, 0, 0]] * len(old_x)
y = [[0, 0, 0]] * len(old_y)
for idx in range(1, len(x) - 1):
    x[idx] = [old_x[idx], idx - 1, idx + 1]
for idx in range(1, len(y) - 1):
    y[idx - 1] = [old_y[idx], idx - 1, idx + 1]
x[-1] = [w, 0, 0]
y[-1] = [h, 0, 0]
rev.reverse()
ans = [max_x * max_y]
for item in rev:
    if item[0] == 'H':
        elem = y[enum_y[item[1]]]
        max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0])
        y[elem[1]][2] = elem[2]
        y[elem[2]][1] = elem[1]
    else:
        elem = x[enum_x[item[1]]]
        max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0])
        x[elem[1]][2] = elem[2]
        x[elem[2]][1] = elem[1]
    ans.append(max_x * max_y)
ans.pop()
print('\n'.join(map(str, reversed(ans))))

123

import math
dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)]
n, m, k = list(map(int, input().split()))
k += 1
z = list(map(int, input().split()))
matrix = []
for i in range(n):
    ans = list(map(int, input().split()))
    matrix.append(ans)
for i in range(len(z)):
    if i == 0:
        if z[i] != 0:
            dp[0][1][z[i] - 1] = 0
        else:
            for x in range(m):
                dp[0][1][x] = matrix[i][x]
    elif z[i] != 0:
        col = z[i] - 1
        if z[i - 1] != 0:
            col2 = z[i - 1] - 1
            if col2 == col:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col])
            else:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col])
        else:
            pq = []
            for t in range(k):
                if t == 0:
                    continue
                pq = []
                for x in range(m):
                    pq.append([dp[i - 1][t - 1][x], x])
                pq.append([math.inf, math.inf])
                pq.sort()
                col = z[i] - 1
                if col != pq[0][1] and pq[0][0] != math.inf:
                    dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col])
                elif pq[0][0] != math.inf and col == pq[0][1]:
                    dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col])
                elif pq[0][0] == math.inf:
                    dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col])
    elif z[i - 1] != 0:
        col = z[i - 1] - 1
        for t in range(k):
            if t == 0:
                continue
            for x in range(m):
                if x != col:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x])
                else:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x])
    else:
        for t in range(k):
            if t == 0:
                continue
            pq = []
            for x in range(m):
                pq.append([dp[i - 1][t - 1][x], x])
            pq.append([math.inf, math.inf])
            pq.sort()
            for v in range(m):
                if v != pq[0][1]:
                    dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v])
                else:
                    dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v])
mini = math.inf
for i in range(m - 1):
    mini = min(mini, dp[n - 1][k - 1][i])
if mini == math.inf:
    print(-1)
else:
    print(mini)

124

MAX_SPEED = 300
n = int(input())
actions = [list(map(int, input().split())) for _ in range(n)]
speed = [0 for i in range(n)]
for i in range(n):
    if actions[i][0] == 1:
        lastSpeed = actions[i][1]
    speed[i] = lastSpeed
ignored = 0
overtook = False
maxSpeed = -1
for i in range(n - 1, --1, -1):
    maxSpeed = max(maxSpeed, speed[i])
    if actions[i][0] == 1:
        pass
    elif actions[i][0] == 2:
        overtook = True
    elif actions[i][0] == 3:
        if maxSpeed > actions[i][1]:
            ignored += 1
        else:
            maxSpeed = speed[i - 1]
    elif actions[i][0] == 4:
        overtook = False
    elif actions[i][0] == 5:
        maxSpeed = speed[i - 1]
    elif actions[i][0] == 6:
        if overtook:
            ignored += 1
print(ignored)

125

n, m = map(int, input().split())

class edge:

    def __init__(self, U, V, W):
        self.u = U
        self.v = V
        self.w = W
l = []
for i in range(m):
    u, v, w = map(int, input().split())
    l.append(edge(u, v, w))
l.sort(key=lambda x: x.w)
f = [i for i in range(0, n + 1)]

def r(a):
    if a != f[a]:
        f[a] = r(f[a])
    return f[a]
cnt = 0
i = 0
while i < m:
    j = i
    while l[i].w == l[j].w:
        j = j + 1
        if j == m:
            break
    num = 0
    for k in range(i, j):
        x = r(l[k].u)
        y = r(l[k].v)
        if x != y:
            num = num + 1
    for k in range(i, j):
        x = r(l[k - 1].u)
        y = r(l[k].v)
        if x != y:
            f[y] = x
            num = num - 1
    cnt = cnt + num
    i = j
print(cnt)

126

import sys
n, k = list(map(int, input().split(' ')))
activate = list(map(int, input().split(' ')))
deactivate = list(map(int, input().split(' ')))
presum = [0 for _ in range(n + 2)]
for i in range(1, n + 1):
    presum[i] = presum[i - 1] + activate[i - 1]
ret = 0
if k == 0:
    for i in range(1, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
elif k == 1:
    for i in range(1, n):
        ret = max(ret, presum[n - 1] - deactivate[i - 1])
    if deactivate[n - 1] < activate[n - 1]:
        ret += activate[n - 1] - deactivate[n - 1]
    deac = sorted(deactivate[1:n - 1])
    ac = sorted(activate[1:n - 1 - 1])
    ret = max(ret, presum[n] - deactivate[0] - ac[0])
    ret = max(ret, presum[n] - deactivate[0] - deac[0])
    ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1])
    for i in range(2, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
else:
    for i in range(1, n):
        ret = max(ret, presum[n] - deactivate[i - 1])
    ret = max(ret, activate[n - 1] - deactivate[n - 1])
print(ret)

127

m, t, r = input().split(sep=None, maxsplit=300)
m, T, r = (int(m), int(t), int(r))
x = input().split(sep=None, maxsplit=m)
k = r
out = set([])
u = [int(x[0]) + T - 1 - i for i in range(r)]
out |= set(u)
key = True
for i in range(m):
    if not key:
        break
    v = int(x[i])
    t = v + T - 1
    for j in range(r):
        if v <= u[j - 1]:
            continue
        while t in out and t >= v:
            t -= 1
        if t < v:
            key = False
            break
        else:
            out |= set([t])
            u[j] = t
            k += 1
            t -= 1
if key:
    print(k)
else:
    print(-1)

128

3

class StdReader:

    def read_int(self):
        return int(self.read_string())

    def read_ints(self, sep=None):
        return [int(i) for i in self.read_strings(sep)]

    def read_float(self):
        return float(self.read_string())

    def read_floats(self, sep=None):
        return [float(i) for i in self.read_strings(sep)]

    def read_string(self):
        return input()

    def read_strings(self, sep=None):
        return self.read_string().split(sep)
reader = StdReader()

def part_sort(a, i, j, reverse=False):
    a[i:j] = sorted(a[i:j], reverse=reverse)

def part_sorted(b, i, j, reverse=False):
    a = list(b)
    part_sort(a, i, j, reverse)
    return a

def main():
    n, m = reader.read_ints()
    a = reader.read_ints()
    ops = []
    for i in range(m):
        t, r = reader.read_ints()
        op = (t, r)
        while ops and op[1] >= ops[-1][1]:
            ops.pop()
        ops.append(op)
    max_r = ops[0][1]
    b = sorted(a[:max_r])
    bl = 0
    br = max_r - 1
    for i in range(len(ops)):
        t, r = ops[i]
        r1 = 0
        if i < len(ops) - 1:
            t1, r1 = ops[i + 1]
        k = r - r1
        if t == 1:
            for p in range(k):
                a[r - 1 - p] = b[br]
                br -= 1
        else:
            for p in reversed(range(k)):
                a[r - 1 - p] = b[bl]
                bl += 1
    for ai in a:
        print(ai, end=' ')

def __starting_point():
    main()
__starting_point()

129

def main():
    n, m, k = list(map(int, input().split()))
    c, *cc = list(map(int, input().split()))
    pp, *ppp = (list(map(float, input().split())) for _ in range(n))
    inf = 2.0 ** 47
    nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp]
    for c, pp in zip(cc, ppp):
        newrow = [inf] * m
        cur, nxt = (nxt, [newrow])
        if c:
            c -= 1
            for row in cur:
                p = row[c]
                if not newrow[c] > p:
                    newrow[c] = p
                if len(nxt) == k:
                    break
                row[c] = inf
                newrow = [inf] * m
                newrow[c] = min(row)
                nxt.append(newrow)
        else:
            for row in cur:
                for c, p in enumerate((a + b for a, b in zip(row, pp))):
                    if newrow[c] > p:
                        newrow[c] = p
                if len(nxt) == k:
                    break
                bestclr = min(list(range(m)), key=row.__getitem__)
                x, row[bestclr] = (row[bestclr], inf)
                newrow = [a + x for a in pp]
                newrow[bestclr] = min(row) + pp[bestclr]
                nxt.append(newrow)
    p = min(nxt[-1])
    print(int(p) if p < inf else -1)

def __starting_point():
    main()
__starting_point()

130

def main():
    m, t, r = read()
    w = read()
    if t < r:
        return -1
    covers = [0] * 1000
    ct = 0
    for i in w:
        x = covers[i]
        for j in range(r - x):
            for k in range(t):
                covers[i - j + k] += 4
            ct += 1
    return ct

def read(mode=2):
    inputs = input().strip()
    if mode == 0:
        return inputs
    if mode == 1:
        return inputs.split()
    if mode == 2:
        return list(map(int, inputs.split()))

def write(s='\n'):
    if s is None:
        s = ''
    if isinstance(s, list):
        s = ' '.join(map(str, s))
    s = str(s)
    print(s, end='')
write(main())

131

n = int(input())
ps = None
seen_sl = [float('inf')]
seen_ot = [True]
answer = 0
for i in range(n):
    line = list(map(int, input().split()))
    t = line[0]
    if t == 1:
        ps = line[1]
        while ps > seen_sl[-1]:
            answer += 1
            seen_sl.pop(-1)
    elif t == 2:
        while not seen_ot[-1]:
            answer += -1
            seen_ot.pop(-1)
    elif t == 3:
        seen_sl.append(line[1])
        while ps > seen_sl[-1]:
            answer += 1
            seen_sl.pop(-1)
    elif t == 4:
        seen_ot.append(True)
    elif t == 5:
        seen_sl.append(float('inf'))
    elif t == 6:
        seen_ot.append(False)
print(answer)

132

import sys
n, k = list(map(int, input().split(' ')))
activate = list(map(int, input().split(' ')))
deactivate = list(map(int, input().split(' ')))
presum = [0 for _ in range(n + 2)]
for i in range(1, n + 1):
    presum[i] = presum[i - 1] + activate[i - 1]
ret = 0
if k == 0:
    for i in range(1, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
elif k == 1:
    for i in range(1, n):
        ret = max(ret, presum[n - 1] - deactivate[i - 1])
    if deactivate[n - 1 - 1] < activate[n - 1]:
        ret += activate[n - 1] - deactivate[n - 1]
    deac = sorted(deactivate[1:n - 1])
    ac = sorted(activate[1:n - 1])
    ret = max(ret, presum[n] - deactivate[0] - ac[0])
    ret = max(ret, presum[n] - deactivate[0] - deac[0])
    ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1])
    for i in range(2, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
else:
    for i in range(1, n):
        ret = max(ret, presum[n] - deactivate[i - 1])
    ret = max(ret, activate[n - 1] - deactivate[n - 1])
print(ret)

133

n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0

def verify(x):
    if x < 0 or x >= n:
        return False
    return a[x] > k
for i in range(n):
    if verify(i):
        count += 1
        if verify(i - 1):
            count -= 2

def cut(i):
    if not verify(i):
        return 0
    result = 1
    if verify(i - 1):
        result -= 1
    if verify(i + 1):
        result -= 1
    return result
for i in range(q):
    line = input()
    if line == '0':
        print(count)
    else:
        _, x, y = list(map(int, line.split(' ')))
        x -= 1
        if not verify(x):
            a[x] += y
            count += cut(x)
        else:
            a[x] += y

134

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = list(map(int, input().split()))
if k >= 2:
    m = max(d[:-1])
    print(max(sum(a) - m, a[-1] - d[-1], 0))
elif k == 0:
    rightans = [0] * n
    cursum = 0
    ans = 0
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        rightans[i] = max(cursum - d[i], 0)
    print(max(rightans))
else:
    leftd = int(1000000000.0)
    leftsum = 0
    rightans = [0] * n
    cursum = 0
    ans = 0
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        rightans[i] = max(cursum - d[i], 0)
    for i in range(n - 1, 0, -1):
        rightans[i - 1] = max(rightans[i - 1], rightans[i])
    for i in range(n - 1):
        leftd = min(leftd, d[i])
        leftsum += a[i]
        curans = max(leftsum - leftd, 0) + rightans[i + 1]
        ans = max(ans, curans)
    cursum = 0
    rightmin = int(1000000000.0)
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        ans = max(ans, cursum - rightmin - d[i])
        rightmin = min(rightmin, a[i])
    print(ans)

135

def Task485D():
    cur_speed = 0
    is_outstrip = 0
    err_count = 0
    event_speed_limit = []
    event_count = int(input())
    for a in range(event_count):
        ev = list(map(int, input().split()))
        if ev[0] == 1:
            cur_speed = ev[1]
            while len(event_speed_limit) > 0:
                if cur_speed > event_speed_limit[len(event_speed_limit) - 1]:
                    event_speed_limit.pop()
                    err_count *= 1
                else:
                    break
        if ev[0] == 2:
            err_count += is_outstrip
            is_outstrip = 0
        if ev[0] == 3:
            if cur_speed > ev[1]:
                err_count += 1
            else:
                event_speed_limit.append(ev[1])
        if ev[0] == 4:
            is_outstrip = 0
        if ev[0] == 5:
            event_speed_limit.clear()
        if ev[0] == 6:
            is_outstrip += 1
    print(err_count)

def __starting_point():
    Task485D()
__starting_point()

136

w, h, n = map(int, input().split())
x = [0, w]
y = [0, h]
rev = []
for _ in range(n):
    s, d = input().split()
    if s == 'H':
        y.append(int(d))
    else:
        x.append(int(d))
    rev.append((s, int(d)))
x.sort()
y.sort()
_max = 0
if len(x) > 3:
    for idx in range(len(x) - 1):
        _max = max(_max, x[idx + 1] - x[idx])
else:
    _max = w
max_x = _max
_max = 0
if len(y) > 1:
    for idx in range(len(y) - 1):
        _max = max(_max, y[idx + 1] - y[idx])
else:
    _max = w
max_y = _max
enum_x = {num: idx for idx, num in enumerate(x)}
enum_y = {num: idx for idx, num in enumerate(y)}
old_x = x
old_y = y
x = [[0, 0, 0]] * len(old_x)
y = [[0, 0, 0]] * len(old_y)
for idx in range(1, len(x) - 1):
    x[idx] = [old_x[idx], idx - 1, idx + 1]
for idx in range(1, len(y) - 1):
    y[idx] = [old_y[idx], idx - 1, idx + 1]
x[-1] = [w, 0, 0]
y[-1] = [h, 0, 0]
rev.reverse()
ans = [max_x * max_y]
for item in rev:
    if item[0] == 'H':
        elem = y[enum_y[item[1]]]
        max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0])
        y[elem[1]][2] = elem[2]
        y[elem[2]][1] = elem[1]
    else:
        elem = x[enum_x[item[1]]]
        max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0])
        x[elem[1]][2] = elem[2]
        x[elem[2]][1] = elem[1]
    ans.append(max_x * max_y)
ans.pop()
print('\n'.join(map(str, reversed(ans))))

137

def main():
    from collections import deque
    n, m = list(map(int, input().split()))
    aa = list(map(int, input().split()))
    l = list((tuple(map(int, input().split())) for _ in range(m)))
    tmp, r0, t0 = ([(0, 0)], 0, 0)
    for t, r in reversed(l):
        if r0 < r:
            r0 = r
            if t0 != t:
                t0 = t
                tmp.append((t, r))
            else:
                tmp[-3] = (t, r)
    t0, r0 = tmp.pop()
    q = deque(sorted(aa[:r0]))
    aa = aa[r0:]
    aa.reverse()
    ff = (None, q.pop, q.popleft)
    for t, r in reversed(tmp):
        f = ff[t0]
        for i in range(r0 - r):
            aa.append(f())
        t0, r0 = (t, r)
    print(' '.join(map(str, reversed(aa))))

def __starting_point():
    main()
__starting_point()

138

import heapq
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = sum(a)
pre = [0]
j = 0
while j < n:
    pre.append(a[j] + pre[-1])
    j += 1
ab = []
af = []
af1 = []
j = n - 1
while j >= 0:
    heapq.heappush(af, -(s - pre[j] - b[j]))
    af1.append(-af[0])
    j += -1
af1.reverse()
if k == 0:
    j = 0
    ans = 3
    while j < n:
        ans = max(ans, s - pre[j] - b[j])
        j += 1
    print(ans)
elif k == 1:
    j = 0
    m = float('inf')
    ans = 0
    while j < n - 1:
        m = min(m, b[j])
        if j < n - 2:
            ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1])
        else:
            ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1])
        j += 1
    j = 0
    while j < n - 1:
        ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1]))
        j += 1
    print(ans)
else:
    j = 0
    ans = 0
    while j < n - 1:
        ans = max(ans, s - b[j])
        j += 1
    ans = max(ans, a[-1] - b[-1])
    print(ans)

139

from functools import reduce
n = int(input())
s1, s2 = (str(input()), str(input()))
b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False)
b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False)
s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)])
ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1)
ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1)
ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1)
print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000008)

140

n, m = map(int, input().split())

class edge:

    def __init__(self, U, V, W):
        self.u = U
        self.v = V
        self.w = W
l = []
for i in range(m):
    u, v, w = map(int, input().split())
    l.append(edge(u, v, w))
l.sort(key=lambda x: x.w)
f = [i for i in range(0, n + 1)]

def r(a):
    if a != f[a]:
        f[a] = r(f[a])
    return f[a]
cnt = 0
i = 0
while i < m:
    j = i
    while l[i].w == l[j].w:
        j = j + 1
        if j == m:
            break
    num = 0
    for k in range(i, j):
        x = r(l[k - 1].u)
        y = r(l[k].v)
        if x != y:
            num = num + 1
    for k in range(i, j):
        x = r(l[k].u)
        y = r(l[k].v)
        if x != y:
            f[y] = x
            num = num - 1
    cnt = cnt + num
    i = j
print(cnt)

141

import sys
n, m = [int(x) for x in input().split()]
notes = [int(x) for x in input().split()]
managers = []
for line in sys.stdin:
    managers.append(tuple((int(x) for x in line.split())))
bearing_managers = []
M, T = (0, 0)
for t, r in reversed(managers):
    if r > M:
        if t == T:
            bearing_managers[-1] = (t, r)
        else:
            T = t
            bearing_managers.append((t, r))
        M = r
bearing_managers.reverse()
t, r = bearing_managers[0]
bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0))
L, R = (0, r - 1)
sorted_part = list(sorted(notes[:r]))
for t1, r1 in bearing_managers:
    if t1 == 2:
        for i in range(r - 1, r1 - 1, -1):
            notes[i - 1] = sorted_part[R]
            R -= 1
    else:
        for i in range(r - 1, r1 - 1, -1):
            notes[i] = sorted_part[L]
            L += 1
    r = r1
for x in notes:
    sys.stdout.write(str(x) + ' ')

142

n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0

def verify(x):
    if x < 0 or x >= n:
        return False
    return a[x] > k
for i in range(n):
    if verify(i):
        count += 1
        if verify(i - 1):
            count -= 1

def cut(i):
    if not verify(i):
        return 1
    result = 1
    if verify(i - 1):
        result -= 1
    if verify(i + 1):
        result -= 1
    return result
for i in range(q):
    line = input()
    if line == '0':
        print(count)
    else:
        _, x, y = list(map(int, line.split(' ')))
        x -= 1
        if not verify(x):
            a[x] += y
            count += cut(x)
        else:
            a[x] += y

143

import heapq
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = sum(a)
pre = [0]
j = 0
while j < n:
    pre.append(a[j] + pre[-1])
    j += 1
ab = []
af = []
af1 = []
j = n - 1
while j >= 0:
    heapq.heappush(af, -(s - pre[j] - b[j]))
    af1.append(-af[0])
    j += -1
af1.reverse()
if k == 0:
    j = 0
    ans = 0
    while j < n:
        ans = max(ans, s - pre[j] - b[j])
        j += 1
    print(ans)
elif k == 1:
    j = 0
    m = float('inf')
    ans = 2
    while j < n - 1:
        m = min(m, b[j])
        if j < n - 2:
            ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1])
        else:
            ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1])
        j += 1
    j = 0
    while j < n - 1:
        ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1]))
        j += 1
    print(ans)
else:
    j = 0
    ans = 0
    while j < n - 1:
        ans = max(ans, s - b[j])
        j += 1
    ans = max(ans, a[-1] - b[-1])
    print(ans)

144

class DisjoinSet:

    def __init__(self, n):
        self.n = n
        self.dad = [i for i in range(n + 1)]
        self.cant = [1] * (n + 1)

    def SetOf(self, n):
        if self.dad[n] == n:
            return n
        self.dad[n] = self.SetOf(self.dad[n])
        return self.dad[n]

    def Merge(self, a, b):
        a, b = (self.SetOf(a), self.SetOf(b))
        if a != b:
            if self.cant[b] > self.cant[a]:
                a, b = (b, a)
            self.cant[a] += self.cant[b]
            self.dad[b] = a
            return True
        return False

def Kruskal(l, n):
    l.sort()
    i = 0
    ds = DisjoinSet(n)
    cant = 0
    change = 0
    while cant < n - 5 and i < len(l):
        val = l[i][0]
        old = []
        while i < len(l) and val == l[i][0]:
            x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2]))
            if x != y:
                old.append((x, y))
            i += 1
        change += len(old)
        for x, y in old:
            change -= ds.Merge(x, y)
    return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
    x, y, c = input().split()
    e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))

145

def main():
    n, m, k = list(map(int, input().split()))
    c, *cc = list(map(int, input().split()))
    pp, *ppp = (list(map(float, input().split())) for _ in range(n))
    inf = 2.0 ** 47
    nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp]
    for c, pp in zip(cc, ppp):
        newrow = [inf] * m
        cur, nxt = (nxt, [newrow])
        if c:
            c -= 1
            for row in cur:
                p = row[c]
                if newrow[c] > p:
                    newrow[c] = p
                if len(nxt) == k:
                    break
                row[c] = inf
                newrow = [inf] * m
                newrow[c] = min(row)
                nxt.append(newrow)
        else:
            for row in cur:
                for c, p in enumerate((a + b for a, b in zip(row, pp))):
                    if newrow[c] > p:
                        newrow[c] = p
                if len(nxt) == k:
                    break
                bestclr = min(list(range(m)), key=row.__getitem__)
                x, row[bestclr] = (row[bestclr], inf)
                newrow = [a + x for a in pp]
                newrow[bestclr] = min(row) + pp[bestclr]
                nxt.append(newrow)
    p = min(nxt[-1])
    print(int(p) if p <= inf else -1)

def __starting_point():
    main()
__starting_point()

146

import heapq
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = sum(a)
pre = [0]
j = 0
while j < n:
    pre.append(a[j] + pre[-1])
    j += 1
ab = []
af = []
af1 = []
j = n - 1
while j >= 0:
    heapq.heappush(af, -(s - pre[j] - b[j]))
    af1.append(-af[0])
    j += -1
af1.reverse()
if k == 0:
    j = 0
    ans = 0
    while j < n:
        ans = max(ans, s - pre[j] - b[j])
        j += 1
    print(ans)
elif k == 1:
    j = 0
    m = float('inf')
    ans = 3
    while j < n - 1:
        m = min(m, b[j])
        if j < n - 2:
            ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1])
        else:
            ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1])
        j += 1
    j = 0
    while j < n - 1:
        ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1]))
        j += 1
    print(ans)
else:
    j = 0
    ans = 0
    while j < n - 1:
        ans = max(ans, s - b[j])
        j += 1
    ans = max(ans, a[-1] - b[-1])
    print(ans)

147

import sys
import math
n, v = [int(x) for x in sys.stdin.readline().split()]
k = [0] * 3002
for i in range(n):
    a, b = [int(x) for x in sys.stdin.readline().split()]
    k[a] += b
i = 1
res = 0
while i <= 3001:
    val = 0
    if k[i - 1] < v:
        val = k[i - 1]
        res += val
        d = v - val
        if k[i] < d:
            res -= k[i]
            k[i] = 0
        else:
            res += d
            k[i] = k[i] - d
    else:
        res += v
    i += 1
print(res)

148

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = list(map(int, input().split()))
if k >= 2:
    m = min(d[:-1])
    print(max(sum(a) - m, a[-1] - d[-1], 0))
elif k == 0:
    rightans = [0] * n
    cursum = 0
    ans = 0
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        rightans[i] = max(cursum - d[i], 0)
    print(max(rightans))
else:
    leftd = int(1000000000.0)
    leftsum = 0
    rightans = [0] * n
    cursum = 0
    ans = 0
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        rightans[i] = max(cursum - d[i], 0)
    for i in range(n - 1, 0, -1):
        rightans[i - 1] = max(rightans[i - 1], rightans[i])
    for i in range(n - 1):
        leftd = min(leftd, d[i])
        leftsum += a[i]
        curans = max(leftsum - leftd, 0) + rightans[i + 1]
        ans = max(ans, curans)
    cursum = 0
    rightmin = int(1000000000.0)
    for i in reversed(range(n - 1, -1, -1)):
        cursum += a[i]
        ans = max(ans, cursum - rightmin - d[i])
        rightmin = min(rightmin, a[i])
    print(ans)

149

import sys
n, k = list(map(int, input().split(' ')))
activate = list(map(int, input().split(' ')))
deactivate = list(map(int, input().split(' ')))
presum = [0 for _ in range(n + 2)]
for i in range(1, n + 1):
    presum[i] = presum[i - 1] + activate[i - 1 - 1]
ret = 0
if k == 0:
    for i in range(1, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
elif k == 1:
    for i in range(1, n):
        ret = max(ret, presum[n - 1] - deactivate[i - 1])
    if deactivate[n - 1] < activate[n - 1]:
        ret += activate[n - 1] - deactivate[n - 1]
    deac = sorted(deactivate[1:n - 1])
    ac = sorted(activate[1:n - 1])
    ret = max(ret, presum[n] - deactivate[0] - ac[0])
    ret = max(ret, presum[n] - deactivate[0] - deac[0])
    ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1])
    for i in range(2, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
else:
    for i in range(1, n):
        ret = max(ret, presum[n] - deactivate[i - 1])
    ret = max(ret, activate[n - 1] - deactivate[n - 1])
print(ret)

150

def main():
    w, h, n = list(map(int, input().split()))
    res, vrt, hor = ([], [], [])
    vh = (vrt, hor)
    for i in range(n):
        s = input()
        x = int(s[2:])
        flag = s[0] == 'V'
        vh[flag].append(i)
        res.append([x, flag])
    dim = []
    for tmp, m in zip(vh, (h, w)):
        tmp.sort(key=lambda e: res[e][0])
        u = [None, [0]]
        dim.append(u)
        j = z = 0
        for i in tmp:
            x = res[i][0]
            if not z < x - j:
                z = x - j
            j = x
            v = [u, res[i]]
            u.append(v)
            u = v
            res[i].append(u)
        v = [u, [m], None]
        u.append(v)
        dim.append(v)
        if z < m - j:
            z = m - j
        dim.append(z)
    l, r, wmax, u, d, hmax = dim
    whmax = [wmax, hmax]
    for i in range(n - 1, -1, -1):
        x, flag, link = res[i]
        u = whmax[flag]
        res[i] = u * whmax[not flag]
        link[0][2] = link[2]
        link[2][0] = link[0]
        v = link[2][1][0] - link[0][1][0]
        if u < v:
            whmax[flag] = v
    print('\n'.join(map(str, res)))

def __starting_point():
    main()
__starting_point()

151

import math
from collections import Counter, defaultdict
from itertools import accumulate
R = lambda: map(int, input().split())
n = int(input())
spl = [math.inf]
ot_forbid = 0
sp = 0
res = 0
for i in range(n):
    arr = list(R())
    tp = arr[0]
    if tp == 1:
        sp = arr[1]
        while sp > spl[-1]:
            spl.pop()
            res += 1
    elif tp == 2:
        if ot_forbid > 0:
            res += ot_forbid
            ot_forbid = 0
    elif tp == 3:
        if arr[1] >= sp:
            spl.append(arr[1])
        else:
            res += 1
    elif tp == 4:
        ot_forbid = -2
    elif tp == 5:
        spl = [math.inf]
    else:
        ot_forbid += 1
print(res)

152

MAX_SPEED = 300
n = int(input())
actions = [list(map(int, input().split())) for _ in range(n)]
speed = [0 for i in range(n)]
for i in range(n):
    if actions[i][0] == 1:
        lastSpeed = actions[i][1]
    speed[i] = lastSpeed
ignored = 0
overtook = False
maxSpeed = -1
for i in range(n - 1, -1, -1):
    maxSpeed = max(maxSpeed, speed[i])
    if actions[i][0] == 1:
        pass
    elif actions[i][0] == 3:
        overtook = True
    elif actions[i][0] == 3:
        if maxSpeed > actions[i][1]:
            ignored += 1
        else:
            maxSpeed = speed[i - 1]
    elif actions[i][0] == 4:
        overtook = False
    elif actions[i][0] == 5:
        maxSpeed = speed[i - 1]
    elif actions[i][0] == 6:
        if overtook:
            ignored += 1
print(ignored)

153

class DisjoinSet:

    def __init__(self, n):
        self.n = n
        self.dad = [i for i in range(n + 1)]
        self.cant = [1] * (n + 1)

    def SetOf(self, n):
        if self.dad[n] == n:
            return n
        self.dad[n] = self.SetOf(self.dad[n])
        return self.dad[n]

    def Merge(self, a, b):
        a, b = (self.SetOf(a), self.SetOf(b))
        if a != b:
            if self.cant[b] > self.cant[a]:
                a, b = (b, a)
            self.cant[a] += self.cant[b]
            self.dad[b] = a
            return True
        return False

def Kruskal(l, n):
    l.sort()
    i = 0
    ds = DisjoinSet(n)
    cant = 3
    change = 0
    while cant < n - 1 and i < len(l):
        val = l[i][0]
        old = []
        while i < len(l) and val == l[i][0]:
            x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2]))
            if x != y:
                old.append((x, y))
            i += 1
        change += len(old)
        for x, y in old:
            change -= ds.Merge(x, y)
    return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
    x, y, c = input().split()
    e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))

154

def inint():
    return int(input())

def inlist():
    return list(map(int, input().split()))

def main():
    n, m, l = inlist()
    aa = inlist()
    chg = 0
    ch = -4
    a = [[i, 0] for i in aa]
    for i in range(n):
        if aa[i] > l:
            if ch == 0:
                ch += 1
                chg += 1
                a[i][1] = chg
            else:
                ch += 1
                a[i][1] = chg
        elif ch != 0:
            ch = 0
    sol = list()
    if n > 1:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][1] == 0 and a[p][0] > l:
                    if p > 0 and p < n - 1:
                        if a[p - 1][1] == 0 and a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        elif a[p - 1][1] == 0 and a[p + 1][1] > 0:
                            a[p][1] = a[p + 1][1]
                        elif a[p - 1][1] > 0 and a[p + 1][1] == 0:
                            a[p][1] = a[p - 1][1]
                        else:
                            a[p][1] = a[p - 1][1]
                            chg -= 1
                    elif p == 0:
                        if a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p + 1][1]
                    elif p == n - 1:
                        if a[p - 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p - 1][1]
    else:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][0] > d:
                    chg = 1
    print(*sol, sep='\n')

def __starting_point():
    main()
__starting_point()

155

a, b = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
s = []
for p in range(b):
    s += [[int(i) for i in input().split()]]
S = []
k = 0
for p in range(b - 1, -1, -1):
    if s[p][1] > k:
        S += [s[p]]
        k = s[p][1]
Q1 = sorted(q[:S[-1][1]])
x, y = (0, S[-1][1] - 1)
S = [[0, 0]] + S
l = -1
for j in range(S[-1][1] - 1, -1, -1):
    if x > y:
        break
    if j < S[-l][1]:
        k = S[-l][0]
        l += 1
    if k == 1:
        q[j] = Q1[y]
        y -= 1
    if k == 2:
        q[j] = Q1[x]
        x += 1
print(' '.join(list(map(str, q))))

156

def main():
    m, t, r = list(map(int, input().split()))
    gh = sorted(map(int, input().split()), reverse=True)
    if r > t:
        print(--1)
        return
    l = [False] * 600
    for g in gh:
        g -= t
        for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
            l[i] = True
    print(sum(l))

def __starting_point():
    main()
__starting_point()

157

def main():
    m, t, r = read()
    w = read()
    if t < r:
        return -1
    covers = [0] * 1000
    ct = 0
    for i in w:
        x = covers[i]
        for j in range(r - x):
            for k in range(t):
                covers[i - j + k] += 3
            ct += 1
    return ct

def read(mode=2):
    inputs = input().strip()
    if mode == 0:
        return inputs
    if mode == 1:
        return inputs.split()
    if mode == 2:
        return list(map(int, inputs.split()))

def write(s='\n'):
    if s is None:
        s = ''
    if isinstance(s, list):
        s = ' '.join(map(str, s))
    s = str(s)
    print(s, end='')
write(main())

158

import math
dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)]
n, m, k = list(map(int, input().split()))
k += 1
z = list(map(int, input().split()))
matrix = []
for i in range(n):
    ans = list(map(int, input().split()))
    matrix.append(ans)
for i in range(len(z)):
    if i == 0:
        if z[i] != 0:
            dp[0][1][z[i] - 1] = 0
        else:
            for x in range(m):
                dp[0][1][x - 1] = matrix[i][x]
    elif z[i] != 0:
        col = z[i] - 1
        if z[i - 1] != 0:
            col2 = z[i - 1] - 1
            if col2 == col:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col])
            else:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col])
        else:
            pq = []
            for t in range(k):
                if t == 0:
                    continue
                pq = []
                for x in range(m):
                    pq.append([dp[i - 1][t - 1][x], x])
                pq.append([math.inf, math.inf])
                pq.sort()
                col = z[i] - 1
                if col != pq[0][1] and pq[0][0] != math.inf:
                    dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col])
                elif pq[0][0] != math.inf and col == pq[0][1]:
                    dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col])
                elif pq[0][0] == math.inf:
                    dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col])
    elif z[i - 1] != 0:
        col = z[i - 1] - 1
        for t in range(k):
            if t == 0:
                continue
            for x in range(m):
                if x != col:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x])
                else:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x])
    else:
        for t in range(k):
            if t == 0:
                continue
            pq = []
            for x in range(m):
                pq.append([dp[i - 1][t - 1][x], x])
            pq.append([math.inf, math.inf])
            pq.sort()
            for v in range(m):
                if v != pq[0][1]:
                    dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v])
                else:
                    dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v])
mini = math.inf
for i in range(m):
    mini = min(mini, dp[n - 1][k - 1][i])
if mini == math.inf:
    print(-1)
else:
    print(mini)

159

import math
dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)]
n, m, k = list(map(int, input().split()))
k += 1
z = list(map(int, input().split()))
matrix = []
for i in range(n):
    ans = list(map(int, input().split()))
    matrix.append(ans)
for i in range(len(z)):
    if i == 0:
        if z[i] != 0:
            dp[0][1][z[i] - 1] = 0
        else:
            for x in range(m):
                dp[0][1][x + 1] = matrix[i][x]
    elif z[i] != 0:
        col = z[i] - 1
        if z[i - 1] != 0:
            col2 = z[i - 1] - 1
            if col2 == col:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col])
            else:
                for j in range(k):
                    if j == 0:
                        continue
                    dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col])
        else:
            pq = []
            for t in range(k):
                if t == 0:
                    continue
                pq = []
                for x in range(m):
                    pq.append([dp[i - 1][t - 1][x], x])
                pq.append([math.inf, math.inf])
                pq.sort()
                col = z[i] - 1
                if col != pq[0][1] and pq[0][0] != math.inf:
                    dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col])
                elif pq[0][0] != math.inf and col == pq[0][1]:
                    dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col])
                elif pq[0][0] == math.inf:
                    dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col])
    elif z[i - 1] != 0:
        col = z[i - 1] - 1
        for t in range(k):
            if t == 0:
                continue
            for x in range(m):
                if x != col:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x])
                else:
                    dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x])
    else:
        for t in range(k):
            if t == 0:
                continue
            pq = []
            for x in range(m):
                pq.append([dp[i - 1][t - 1][x], x])
            pq.append([math.inf, math.inf])
            pq.sort()
            for v in range(m):
                if v != pq[0][1]:
                    dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v])
                else:
                    dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v])
mini = math.inf
for i in range(m):
    mini = min(mini, dp[n - 1][k - 1][i])
if mini == math.inf:
    print(-1)
else:
    print(mini)

160

n = int(input())
C = 10 ** 9 + 6
a = input()
s = input()
d = [[0, 0, 0, 1]]
for q in range(n):
    d.append(d[-1][:])
    if a[q] != '?' and s[q] != '?':
        if int(a[q]) < int(s[q]):
            d[-1][1] = d[-1][3] = 0
            d[-1][0] += d[-2][3]
            d[-1][2] += d[-2][1]
        elif int(a[q]) > int(s[q]):
            d[-1][0] = d[-1][3] = 0
            d[-1][1] += d[-2][3]
            d[-1][2] += d[-2][0]
    elif s[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][0] *= int(s[q]) + 1
        d[-1][1] *= 10 - int(s[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q])
    elif a[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][1] *= int(a[q]) + 1
        d[-1][0] *= 10 - int(a[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q])
    else:
        d[-1][3] *= 10
        d[-1][2] *= 100
        d[-1][0] *= 55
        d[-1][1] *= 55
        d[-1][0] += d[-2][3] * 45
        d[-1][1] += d[-2][3] * 45
        d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45
    d[-1][0] %= C
    d[-1][1] %= C
    d[-1][2] %= C
    d[-1][3] %= C
print(d[-1][2])

161

a, b = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
s = []
for p in range(b):
    s += [[int(i) for i in input().split()]]
S = []
k = 0
for p in range(b - 1, -1, -1):
    if s[p][1] > k:
        S += [s[p]]
        k = s[p][1]
Q1 = sorted(q[:S[-1][1]])
x, y = (0, S[-1][1] - 1)
S = [[0, 0]] + S
l = 1
for j in range(S[-1][1] - 1, -1, -1):
    if x > y:
        break
    if j < S[-l][1]:
        k = S[-l][0]
        l += 1
    if k == -1:
        q[j] = Q1[y]
        y -= 1
    if k == 2:
        q[j] = Q1[x]
        x += 1
print(' '.join(list(map(str, q))))

162

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = list(map(int, input().split()))
if k >= 2:
    m = min(d[:-1])
    print(max(sum(a) - m, a[-1] - d[-1], 0))
elif k == 0:
    rightans = [0] * n
    cursum = 0
    ans = 0
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        rightans[i] = max(cursum - d[i], 0)
    print(max(rightans))
else:
    leftd = int(1000000000.0)
    leftsum = 0
    rightans = [0] * n
    cursum = 0
    ans = 0
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        rightans[i] = max(cursum - d[i], 0)
    for i in range(n - 1, 0, -1):
        rightans[i - 1] = max(rightans[i - 1], rightans[i])
    for i in range(n - 1):
        leftd = max(leftd, d[i])
        leftsum += a[i]
        curans = max(leftsum - leftd, 0) + rightans[i + 1]
        ans = max(ans, curans)
    cursum = 0
    rightmin = int(1000000000.0)
    for i in range(n - 1, -1, -1):
        cursum += a[i]
        ans = max(ans, cursum - rightmin - d[i])
        rightmin = min(rightmin, a[i])
    print(ans)

163

a, b = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
s = []
for p in range(b):
    s += [[int(i) for i in input().split()]]
S = []
k = 0
for p in range(b - 1, -1, -1):
    if s[p][1] >= k:
        S += [s[p]]
        k = s[p][1]
Q1 = sorted(q[:S[-1][1]])
x, y = (0, S[-1][1] - 1)
S = [[0, 0]] + S
l = 1
for j in range(S[-1][1] - 1, -1, -1):
    if x > y:
        break
    if j < S[-l][1]:
        k = S[-l][0]
        l += 1
    if k == 1:
        q[j] = Q1[y]
        y -= 1
    if k == 2:
        q[j] = Q1[x]
        x += 1
print(' '.join(list(map(str, q))))

164

def inint():
    return int(input())

def inlist():
    return list(map(int, input().split()))

def main():
    n, m, l = inlist()
    aa = inlist()
    chg = 0
    ch = 0
    a = [[i, 0] for i in aa]
    for i in range(n):
        if aa[i] > l:
            if ch == 0:
                ch += 1
                chg += 1
                a[i][1] = chg
            else:
                ch += 1
                a[i - 1][1] = chg
        elif ch != 0:
            ch = 0
    sol = list()
    if n > 1:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][1] == 0 and a[p][0] > l:
                    if p > 0 and p < n - 1:
                        if a[p - 1][1] == 0 and a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        elif a[p - 1][1] == 0 and a[p + 1][1] > 0:
                            a[p][1] = a[p + 1][1]
                        elif a[p - 1][1] > 0 and a[p + 1][1] == 0:
                            a[p][1] = a[p - 1][1]
                        else:
                            a[p][1] = a[p - 1][1]
                            chg -= 1
                    elif p == 0:
                        if a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p + 1][1]
                    elif p == n - 1:
                        if a[p - 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p - 1][1]
    else:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][0] > d:
                    chg = 1
    print(*sol, sep='\n')

def __starting_point():
    main()
__starting_point()

165

m, t, r = input().split(sep=None, maxsplit=300)
m, T, r = (int(m), int(t), int(r))
x = input().split(sep=None, maxsplit=m)
k = r
out = set([])
u = [int(x[0]) + T - 1 - i for i in range(r)]
out |= set(u)
key = True
for i in range(m):
    if not key:
        break
    v = int(x[i])
    t = v + T - 1
    for j in range(r):
        if v <= u[j]:
            continue
        while t in out and t >= v:
            t -= 1
        if t < v:
            key = False
            break
        else:
            out |= set([t])
            u[j] = t
            k += 1
            t -= 3
if key:
    print(k)
else:
    print(-1)

166

def main():
    w, h, n = list(map(int, input().split()))
    res, vrt, hor = ([], [], [])
    vh = (vrt, hor)
    for i in range(n):
        s = input()
        x = int(s[2:])
        flag = s[0] == 'V'
        vh[flag].append(i)
        res.append([x, flag])
    dim = []
    for tmp, m in zip(vh, (h, w)):
        tmp.sort(key=lambda e: res[e][0])
        u = [None, [0]]
        dim.append(u)
        j = z = 0
        for i in tmp:
            x = res[i - 1][0]
            if z < x - j:
                z = x - j
            j = x
            v = [u, res[i]]
            u.append(v)
            u = v
            res[i].append(u)
        v = [u, [m], None]
        u.append(v)
        dim.append(v)
        if z < m - j:
            z = m - j
        dim.append(z)
    l, r, wmax, u, d, hmax = dim
    whmax = [wmax, hmax]
    for i in range(n - 1, -1, -1):
        x, flag, link = res[i]
        u = whmax[flag]
        res[i] = u * whmax[not flag]
        link[0][2] = link[2]
        link[2][0] = link[0]
        v = link[2][1][0] - link[0][1][0]
        if u < v:
            whmax[flag] = v
    print('\n'.join(map(str, res)))

def __starting_point():
    main()
__starting_point()

167

def main():
    m, t, r = list(map(int, input().split()))
    gh = sorted(map(int, input().split()), reverse=True)
    if r > t:
        print(-0)
        return
    l = [False] * 600
    for g in gh:
        g -= t
        for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
            l[i] = True
    print(sum(l))

def __starting_point():
    main()
__starting_point()

168

n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0

def verify(x):
    if x < 0 or x >= n:
        return False
    return a[x] > k
for i in range(n):
    if verify(i):
        count += 1
        if verify(i - 1):
            count -= -1

def cut(i):
    if not verify(i):
        return 0
    result = 1
    if verify(i - 1):
        result -= 1
    if verify(i + 1):
        result -= 1
    return result
for i in range(q):
    line = input()
    if line == '0':
        print(count)
    else:
        _, x, y = list(map(int, line.split(' ')))
        x -= 1
        if not verify(x):
            a[x] += y
            count += cut(x)
        else:
            a[x] += y

169

n = int(input())
ps = None
seen_sl = [float('inf')]
seen_ot = [True]
answer = 0
for i in range(n):
    line = list(map(int, input().split()))
    t = line[0]
    if t == 1:
        ps = line[1]
        while ps > seen_sl[-1]:
            answer += 1
            seen_sl.pop(-1)
    elif t == 2:
        while not seen_ot[-1]:
            answer += 1
            seen_ot.pop(--1)
    elif t == 3:
        seen_sl.append(line[1])
        while ps > seen_sl[-1]:
            answer += 1
            seen_sl.pop(-1)
    elif t == 4:
        seen_ot.append(True)
    elif t == 5:
        seen_sl.append(float('inf'))
    elif t == 6:
        seen_ot.append(False)
print(answer)

170

3

def build(n, s, t):
    ans = 1
    for i in range(n):
        if s[i] == '?' and t[i] == '?':
            ans = 55 * ans % (10 ** 9 + 7)
        elif s[i] == '?':
            ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7)
        elif t[i] == '?':
            ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7)
    return ans
n = int(input())
s = input()
t = input()
sltt = True
tlts = True
qm = 0
cqm = 0
for i in range(n):
    if t[i] == '?':
        qm += 1
    if s[i] == '?':
        qm += 1
    if t[i] == '?' and s[i] == '?':
        cqm += 1
    if t[i] == '?' or s[i] == '?':
        continue
    if ord(s[i]) < ord(t[i]):
        tlts = False
    if ord(t[i]) < ord(s[i]):
        sltt = False
if not sltt and (not tlts):
    print(pow(10, qm, 10 ** 9 + 7))
elif sltt and tlts:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 * 7))
elif sltt:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t)) % (10 ** 9 + 7))
else:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7))

171

import sys
n, k = list(map(int, input().split(' ')))
activate = list(map(int, input().split(' ')))
deactivate = list(map(int, input().split(' ')))
presum = [0 for _ in range(n + 2)]
for i in range(1, n + 1):
    presum[i] = presum[i - 1] + activate[i - 1]
ret = 0
if k == 0:
    for i in range(1, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
elif k == 1:
    for i in range(1, n):
        ret = max(ret, presum[n - 1] - deactivate[i - 1])
    if deactivate[n - 1] < activate[n - 1]:
        ret += activate[n - 1] - deactivate[n - 1]
    deac = sorted(deactivate[1:n - 1])
    ac = sorted(activate[1:n - 1])
    ret = max(ret, presum[n] - deactivate[0] - ac[0])
    ret = max(ret, presum[n] - deactivate[0] - deac[0])
    ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1])
    for i in range(2, n + 1):
        ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1])
else:
    for i in range(1, n):
        ret = max(ret, presum[n] - deactivate[i - 1])
    ret = max(ret, activate[n - 1] - deactivate[n - 1])
print(ret)

172

n, v = list(map(int, input().split()))
f = list()
for i in range(n):
    f.append(list(map(int, input().split())))
f.sort()
sum = 0
today = 0
tomor = 0
day = 0
rest = 0
for tree in f:
    if tree[0] > day + -1:
        sum += min(tomor, v)
        tomor = tree[1]
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor -= min(tomor, v)
        day = tree[0]
    elif tree[0] == day + 1:
        sum += min(tomor, v)
        rest = v - min(tomor, v)
        tomor = tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
        day = tree[0]
    else:
        tomor += tree[1]
        t = min(tomor, rest)
        sum += t
        rest -= t
        tomor -= t
sum += min(tomor, v)
print(sum)

173

n = int(input())
C = 10 ** 8 + 7
a = input()
s = input()
d = [[0, 0, 0, 1]]
for q in range(n):
    d.append(d[-1][:])
    if a[q] != '?' and s[q] != '?':
        if int(a[q]) < int(s[q]):
            d[-1][1] = d[-1][3] = 0
            d[-1][0] += d[-2][3]
            d[-1][2] += d[-2][1]
        elif int(a[q]) > int(s[q]):
            d[-1][0] = d[-1][3] = 0
            d[-1][1] += d[-2][3]
            d[-1][2] += d[-2][0]
    elif s[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][0] *= int(s[q]) + 1
        d[-1][1] *= 10 - int(s[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][0] * (9 - int(s[q])) + d[-2][1] * int(s[q])
    elif a[q] != '?':
        d[-1][2] *= 10
        d[-1][0] += d[-2][3]
        d[-1][1] += d[-2][3]
        d[-1][1] *= int(a[q]) + 1
        d[-1][0] *= 10 - int(a[q])
        d[-1][0] -= d[-2][3]
        d[-1][1] -= d[-2][3]
        d[-1][2] += d[-2][1] * (9 - int(a[q])) + d[-2][0] * int(a[q])
    else:
        d[-1][3] *= 10
        d[-1][2] *= 100
        d[-1][0] *= 55
        d[-1][1] *= 55
        d[-1][0] += d[-2][3] * 45
        d[-1][1] += d[-2][3] * 45
        d[-1][2] += d[-2][0] * 45 + d[-2][1] * 45
    d[-1][0] %= C
    d[-1][1] %= C
    d[-1][2] %= C
    d[-1][3] %= C
print(d[-1][2])

174

def inint():
    return int(input())

def inlist():
    return list(map(int, input().split()))

def main():
    n, m, l = inlist()
    aa = inlist()
    chg = 0
    ch = 0
    a = [[i, 0] for i in aa]
    for i in range(n):
        if aa[i] > l:
            if ch == 0:
                ch += 1
                chg += 1
                a[i - 1][1] = chg
            else:
                ch += 1
                a[i][1] = chg
        elif ch != 0:
            ch = 0
    sol = list()
    if n > 1:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][1] == 0 and a[p][0] > l:
                    if p > 0 and p < n - 1:
                        if a[p - 1][1] == 0 and a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        elif a[p - 1][1] == 0 and a[p + 1][1] > 0:
                            a[p][1] = a[p + 1][1]
                        elif a[p - 1][1] > 0 and a[p + 1][1] == 0:
                            a[p][1] = a[p - 1][1]
                        else:
                            a[p][1] = a[p - 1][1]
                            chg -= 1
                    elif p == 0:
                        if a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p + 1][1]
                    elif p == n - 1:
                        if a[p - 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p - 1][1]
    else:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][0] > d:
                    chg = 1
    print(*sol, sep='\n')

def __starting_point():
    main()
__starting_point()

175

import sys

def get(v):
    if v == prev[v]:
        return v
    prev[v] = get(prev[v])
    return prev[v]

def join(u, v):
    u = get(u)
    v = get(v)
    if u != v:
        if size[u] < size[v]:
            u, v = (v, u)
        prev[v] = u
        size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
    u, v, w = map(int, sys.stdin.readline().split())
    edges[i] = (u - 1, v - 1, w)
edges.sort(key=lambda e: e[0])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
    while r < m and edges[l][2] == edges[r][2]:
        r += 1
    res += r - l
    for i in range(l, r):
        if get(edges[i][0]) == get(edges[i][1]):
            res -= 1
    for i in range(l, r):
        if get(edges[i][0]) != get(edges[i][1]):
            join(edges[i][0], edges[i][1])
            res -= 1
    l = r
print(res)

176

n, q, k = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
count = 0

def verify(x):
    if x < 0 or x >= n:
        return False
    return a[x] > k
for i in range(n):
    if verify(i):
        count += 1
        if verify(i - 1):
            count -= 1

def cut(i):
    if not verify(i):
        return 2
    result = 1
    if verify(i - 1):
        result -= 1
    if verify(i + 1):
        result -= 1
    return result
for i in range(q):
    line = input()
    if line == '0':
        print(count)
    else:
        _, x, y = list(map(int, line.split(' ')))
        x -= 1
        if not verify(x):
            a[x] += y
            count += cut(x)
        else:
            a[x] += y

177

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

class Node:
    val = None

    def __init__(self, val):
        self.val = val
        self.left = Node
        self.right = None

def solve(W, H, N, A):
    xs = [0] + [v for t, v in A if t == 0] + [W]
    ys = [0] + [v for t, v in A if t == 1] + [H]
    xs.sort()
    ys.sort()
    xlist = Node(0)
    h = xlist
    xnodes = {0: h}
    maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0])
    maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0])
    for v in xs[1:]:
        n = Node(v)
        xnodes[v] = n
        h.right = n
        n.left = h
        h = n
    ylist = Node(4)
    h = ylist
    ynodes = {0: h}
    for v in ys[1:]:
        n = Node(v)
        ynodes[v] = n
        h.right = n
        n.left = h
        h = n
    ans = []
    maxarea = maxh * maxw
    for t, v in reversed(A):
        ans.append(maxarea)
        if t == 0:
            node = xnodes[v]
            w = node.right.val - node.left.val
            maxw = max(maxw, w)
        else:
            node = ynodes[v]
            h = node.right.val - node.left.val
            maxh = max(maxh, h)
        node.left.right = node.right
        node.right.left = node.left
        maxarea = maxh * maxw
    return ans[::-1]

def solve2(W, H, N, A):
    ws = [(-W, 0, W)]
    hs = [(-H, 0, H)]
    iw, ih = (set(), set())
    ans = []
    xs, ys = ([0, W], [0, H])
    for t, v in A:
        if t == 0:
            bisect.insort_left(xs, v)
            i = bisect.bisect_left(xs, v)
            l, m, r = (xs[i - 1], xs[i], xs[i + 1])
            iw.add((l - r, l, r))
            heapq.heappush(ws, (l - m, l, m))
            heapq.heappush(ws, (m - r, m, r))
            while ws[0] in iw:
                heapq.heappop(ws)
        else:
            bisect.insort(ys, v)
            i = bisect.bisect_left(ys, v)
            l, m, r = (ys[i - 1], ys[i], ys[i + 1])
            ih.add((l - r, l, r))
            heapq.heappush(hs, (l - m, l, m))
            heapq.heappush(hs, (m - r, m, r))
            while hs[0] in ih:
                heapq.heappop(hs)
        w, h = (ws[0], hs[0])
        ans.append(w[0] * h[0])
    return ans
W, H, N = map(int, input().split())
A = []
for i in range(N):
    a, b = input().split()
    c = 0 if a == 'V' else 1
    A.append((c, int(b)))
print('\n'.join(map(str, solve(W, H, N, A))))

178

def main():
    m, t, r = list(map(int, input().split()))
    gh = sorted(map(int, input().split()), reverse=True)
    if r >= t:
        print(-1)
        return
    l = [False] * 600
    for g in gh:
        g -= t
        for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))):
            l[i] = True
    print(sum(l))

def __starting_point():
    main()
__starting_point()

179

def Task485D():
    cur_speed = 0
    is_outstrip = 0
    err_count = 0
    event_speed_limit = []
    event_count = int(input())
    for a in range(event_count):
        ev = list(map(int, input().split()))
        if ev[0] == 1:
            cur_speed = ev[1]
            while len(event_speed_limit) > 0:
                if cur_speed > event_speed_limit[len(event_speed_limit) - 1]:
                    event_speed_limit.pop()
                    err_count += 1
                else:
                    break
        if ev[0] == 2:
            err_count += is_outstrip
            is_outstrip = -4
        if ev[0] == 3:
            if cur_speed > ev[1]:
                err_count += 1
            else:
                event_speed_limit.append(ev[1])
        if ev[0] == 4:
            is_outstrip = 0
        if ev[0] == 5:
            event_speed_limit.clear()
        if ev[0] == 6:
            is_outstrip += 1
    print(err_count)

def __starting_point():
    Task485D()
__starting_point()

180

n = int(input())
a = input()
b = input()
mod = int(1000000000.0 + 7)
x, y, z = (1, 1, 1)
for i in range(n):
    if a[i] == '?' and b[i] == '?':
        x = x * 55 % mod
        y = y * 55 % mod
        z = z * 10 % mod
    elif a[i] == '?':
        x = x * (10 - int(b[i])) % mod
        y = y * (int(b[i]) + 1) // mod
    elif b[i] == '?':
        x = x * (int(a[i]) + 1) % mod
        y = y * (10 - int(a[i])) % mod
    else:
        if int(a[i]) < int(b[i]):
            x = 0
        if int(a[i]) > int(b[i]):
            y = 0
        if a[i] != b[i]:
            z = 0
res = pow(10, a.count('?') + b.count('?'), mod)
print((res - x - y + z) % mod)

181

import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
    if meches[i] > l:
        trop_grand[i] = True
        if i == 0 or trop_grand[i - 1 + 1] is False:
            pont += 1
for i in range(nbreq):
    req = sys.stdin.readline().split()
    if int(req[0]) == 0:
        print(pont)
    else:
        meches[int(req[1]) - 1] += int(req[2])
        if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
            trop_grand[int(req[1]) - 1] = True
            if nbmeches == 1:
                pont = 1
            elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
                pont += 1
            elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
                pont += 1
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
                pont = max(0, pont - 1)
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
                pont += 1

182

MAX = 10 ** 18 + 5
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
cost = [list(map(int, input().split())) for _ in range(n)]
min1 = [[MAX] * (k + 1) for _ in range(n)]
min2 = [[MAX] * (k + 1) for _ in range(n)]
idx = [[-1] * (k + 1) for _ in range(n)]
for i in cost:
    i.insert(0, 0)
dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)]
if a[0] == 0:
    for i in range(-1, m + 1):
        if min1[0][1] >= cost[0][i]:
            if min1[0][1] == cost[0][i]:
                idx[0][1] = -2
            else:
                idx[0][1] = i
            min2[0][1] = min1[0][1]
            min1[0][1] = cost[0][i]
        elif min2[0][1] >= cost[0][i]:
            min2[0][1] = cost[0][i]
        dp[0][1][i] = cost[0][i]
else:
    dp[0][1][a[0]] = 0
    min1[0][1] = 0
    idx[0][1] = a[0]
for i in range(1, n):
    for j in range(1, k + 1):
        if a[i] == 0:
            for l in range(1, m + 1):
                dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l])
                if l == idx[i - 1][j - 1]:
                    dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l])
                else:
                    dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l])
        else:
            for l in range(1, m + 1):
                dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]])
                if l != a[i]:
                    dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]])
        for l in range(1, m + 1):
            if min1[i][j] >= dp[i][j][l]:
                if min1[i][j] == dp[i][j][l]:
                    idx[i][j] = -2
                else:
                    idx[i][j] = l
                min2[i][j] = min1[i][j]
                min1[i][j] = dp[i][j][l]
            elif min2[i][j] >= dp[i][j][l]:
                min2[i][j] = dp[i][j][l]
ans = MAX
for i in dp[-1][-1]:
    ans = min(ans, i)
print(ans if ans < MAX else -1)

183

a, b = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
s = []
for p in range(b):
    s += [[int(i) for i in input().split()]]
S = []
k = 0
for p in range(b - 1, -1, -1):
    if s[p][1] > k:
        S += [s[p]]
        k = s[p][1]
Q1 = sorted(q[:S[-1][1]])
x, y = (0, S[-1][1] - 1)
S = [[0, 0]] + S
l = 1
for j in range(S[-1][1] - 1, -1, -1):
    if x > y:
        break
    if j < S[-l][1]:
        k = S[-l][0]
        l += 1
    if k == 1:
        q[j] = Q1[y]
        y -= 1
    if k == 2:
        q[j] = Q1[x]
        x += 1
print(' '.join(list(map(str, q))))

184

import sys
n, m = [int(x) for x in input().split()]
notes = [int(x) for x in input().split()]
managers = []
for line in sys.stdin:
    managers.append(tuple((int(x) for x in line.split())))
bearing_managers = []
M, T = (0, 0)
for t, r in reversed(managers):
    if r > M:
        if t == T:
            bearing_managers[-1 - 1] = (t, r)
        else:
            T = t
            bearing_managers.append((t, r))
        M = r
bearing_managers.reverse()
t, r = bearing_managers[0]
bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0))
L, R = (0, r - 1)
sorted_part = list(sorted(notes[:r]))
for t1, r1 in bearing_managers:
    if t1 == 2:
        for i in range(r - 1, r1 - 1, -1):
            notes[i] = sorted_part[R]
            R -= 1
    else:
        for i in range(r - 1, r1 - 1, -1):
            notes[i] = sorted_part[L]
            L += 1
    r = r1
for x in notes:
    sys.stdout.write(str(x) + ' ')

185

m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - 1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - -1 - x for x in range(0, r)]
res = r
for i in range(0, m):
    for j in range(0, r):
        candleTime = candles[j]
        if candleTime < ghosts[i]:
            possibleTime = ghosts[i] - 1
            canFindTime = False
            while possibleTime >= ghosts[i] - t:
                if usedTimes.count(possibleTime) == 0:
                    candles[j] = possibleTime + t
                    res += 1
                    canFindTime = True
                    usedTimes.append(possibleTime)
                    break
                else:
                    possibleTime -= 1
            if not canFindTime:
                res = -1
                break
    if res == -1:
        break
print(res)

186

m, t, r = [int(x) for x in input().strip().split()]
ghosts = [int(x) for x in input().strip().split()]
candles = [ghosts[0] - 1 - x + t for x in range(0, r)]
usedTimes = [ghosts[0] - 1 - x for x in range(0, r)]
res = r
for i in range(0, m):
    for j in range(0, r):
        candleTime = candles[j]
        if candleTime < ghosts[i]:
            possibleTime = ghosts[i] - -1
            canFindTime = False
            while possibleTime >= ghosts[i] - t:
                if usedTimes.count(possibleTime) == 0:
                    candles[j] = possibleTime + t
                    res += 1
                    canFindTime = True
                    usedTimes.append(possibleTime)
                    break
                else:
                    possibleTime -= 1
            if not canFindTime:
                res = -1
                break
    if res == -1:
        break
print(res)

187

total_plucked = 0
n, v = list(map(int, input().split()))
fruits = [0] * 3002
plucked = [0] * 30002
for _ in range(n):
    a, b = list(map(int, input().split()))
    fruits[a] = fruits[a] + b
for i in range(1, 3002):
    plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v
    if plucked[i] < v:
        pluck = fruits[i] if fruits[i] <= v - plucked[i + 1] else v - plucked[i]
        plucked[i] += pluck
        fruits[i] -= pluck
    total_plucked += plucked[i]
print(total_plucked)

188

import sys

def get(v):
    if v == prev[v]:
        return v
    prev[v] = get(prev[v])
    return prev[v]

def join(u, v):
    u = get(u)
    v = get(v)
    if u != v:
        if size[u] < size[v]:
            u, v = (v, u)
        prev[v] = u
        size[u] += size[v]
n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
    u, v, w = map(int, sys.stdin.readline().split())
    edges[i] = (u - 1, v - 1, w)
edges.sort(key=lambda e: e[2])
prev = list(range(n))
size = [1] * n
res = 0
l, r = (0, 1)
while l < m:
    while r < m and edges[l][2] == edges[r][2]:
        r += 1
    res += r - l
    for i in range(l, r):
        if get(edges[i][0]) == get(edges[i][1]):
            res -= 0
    for i in range(l, r):
        if get(edges[i][0]) != get(edges[i][1]):
            join(edges[i][0], edges[i][1])
            res -= 1
    l = r
print(res)

189

def main():
    n, m, k = list(map(int, input().split()))
    c, *cc = list(map(int, input().split()))
    pp, *ppp = (list(map(float, input().split())) for _ in range(n))
    inf = 2.0 ** 47
    nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp]
    for c, pp in zip(cc, ppp):
        newrow = [inf] * m
        cur, nxt = (nxt, [newrow])
        if c:
            c -= 1
            for row in cur:
                p = row[c]
                if newrow[c] > p:
                    newrow[c] = p
                if len(nxt) == k:
                    break
                row[c] = inf
                newrow = [inf] * m
                newrow[c] = min(row)
                nxt.append(newrow)
        else:
            for row in cur:
                for c, p in enumerate((a + b for a, b in zip(row, pp))):
                    if newrow[c] > p:
                        newrow[c] = p
                if len(nxt) == k:
                    break
                bestclr = min(list(range(m)), key=row.__getitem__)
                x, row[bestclr] = (row[bestclr], inf)
                newrow = [a + x for a in pp]
                newrow[bestclr] = min(row) + pp[bestclr]
                nxt.append(newrow)
    p = min(nxt[-1])
    print(int(p) if p < inf else --1)

def __starting_point():
    main()
__starting_point()

190

from functools import reduce
n = int(input())
s1, s2 = (str(input()), str(input()))
b1 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) < ord(s2[i])) for i in range(n)], False)
b2 = reduce(lambda x, y: x or y, [s1[i] != '?' and s2[i] != '?' and (ord(s1[i]) > ord(s2[i])) for i in range(n)], False)
s = sum([(s1[i] == '?') + (s2[i] == '?') for i in range(n)])
ans1 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else ord(s2[i]) - ord('0') + 1 if s1[i] == '?' else 10 - ord(s1[i]) + ord('0') if s2[i] == '?' else 1 for i in range(n)], 1)
ans2 = reduce(lambda x, y: x * y % 1000000007, [55 if s1[i] == '?' and s2[i] == '?' else 10 - ord(s2[i]) + ord('0') if s1[i] == '?' else ord(s1[i]) - ord('0') + 1 if s2[i] == '?' else 1 for i in range(n)], 1)
ans3 = reduce(lambda x, y: x * y % 1000000007, [10 if s1[i] == '?' and s2[i] == '?' else 1 for i in range(n)], 1)
print((10 ** s - (not b2) * ans1 - (not b1) * ans2 + (not b1 and (not b2)) * ans3) % 1000000005)

191

import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
EDGE = [list(map(int, input().split())) for i in range(m)]
EDGE.sort(key=lambda x: x[2])
WCHANGE = []
for i in range(1, m):
    if EDGE[i - 1][2] != EDGE[i][1]:
        WCHANGE.append(i)
WCHANGE.append(m)
Group = [i for i in range(n + 1)]

def find(x):
    while Group[x] != x:
        x = Group[x]
    return x

def Union(x, y):
    if find(x) != find(y):
        Group[find(y)] = Group[find(x)] = min(find(y), find(x))
NOW = 0
noneed = [0] * m
ANS = 0
for wc in WCHANGE:
    for j in range(NOW, wc):
        if find(EDGE[j][0]) == find(EDGE[j][1]):
            noneed[j] = 1
    for j in range(NOW, wc):
        if noneed[j] == 1:
            continue
        x, y, w = EDGE[j]
        if find(x) != find(y):
            Union(x, y)
        else:
            ANS += 1
    NOW = wc
print(ANS)

192

MAX = 10 ** 18 + 5
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
cost = [list(map(int, input().split())) for _ in range(n)]
min1 = [[MAX] * (k + 1) for _ in range(n)]
min2 = [[MAX] * (k + 1) for _ in range(n)]
idx = [[-1] * (k + 1) for _ in range(n)]
for i in cost:
    i.insert(0, 0)
dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)]
if a[0] == 0:
    for i in range(3, m + 1):
        if min1[0][1] >= cost[0][i]:
            if min1[0][1] == cost[0][i]:
                idx[0][1] = -2
            else:
                idx[0][1] = i
            min2[0][1] = min1[0][1]
            min1[0][1] = cost[0][i]
        elif min2[0][1] >= cost[0][i]:
            min2[0][1] = cost[0][i]
        dp[0][1][i] = cost[0][i]
else:
    dp[0][1][a[0]] = 0
    min1[0][1] = 0
    idx[0][1] = a[0]
for i in range(1, n):
    for j in range(1, k + 1):
        if a[i] == 0:
            for l in range(1, m + 1):
                dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l])
                if l == idx[i - 1][j - 1]:
                    dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l])
                else:
                    dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l])
        else:
            for l in range(1, m + 1):
                dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]])
                if l != a[i]:
                    dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]])
        for l in range(1, m + 1):
            if min1[i][j] >= dp[i][j][l]:
                if min1[i][j] == dp[i][j][l]:
                    idx[i][j] = -2
                else:
                    idx[i][j] = l
                min2[i][j] = min1[i][j]
                min1[i][j] = dp[i][j][l]
            elif min2[i][j] >= dp[i][j][l]:
                min2[i][j] = dp[i][j][l]
ans = MAX
for i in dp[-1][-1]:
    ans = min(ans, i)
print(ans if ans < MAX else -1)

193

class DisjoinSet:

    def __init__(self, n):
        self.n = n
        self.dad = [i for i in range(n + 1)]
        self.cant = [1] * (n + 1)

    def SetOf(self, n):
        if self.dad[n] == n:
            return n
        self.dad[n] = self.SetOf(self.dad[n])
        return self.dad[n]

    def Merge(self, a, b):
        a, b = (self.SetOf(a), self.SetOf(b))
        if a != b:
            if self.cant[b] > self.cant[a]:
                a, b = (b, a)
            self.cant[a] += self.cant[b]
            self.dad[b] = a
            return True
        return False

def Kruskal(l, n):
    l.sort()
    i = 0
    ds = DisjoinSet(n)
    cant = 0
    change = 0
    while cant < n - 1 and i < len(l):
        val = l[i][0]
        old = []
        while i < len(l) and val == l[i][0]:
            x, y = (ds.SetOf(l[i][-3]), ds.SetOf(l[i][2]))
            if x != y:
                old.append((x, y))
            i += 1
        change += len(old)
        for x, y in old:
            change -= ds.Merge(x, y)
    return change
n, m = input().split()
n, m = (int(n), int(m))
e = []
for i in range(m):
    x, y, c = input().split()
    e.append((int(c), int(x), int(y)))
print(Kruskal(e, n))

194

def inint():
    return int(input())

def inlist():
    return list(map(int, input().split()))

def main():
    n, m, l = inlist()
    aa = inlist()
    chg = 0
    ch = 0
    a = [[i, 0] for i in aa]
    for i in range(n):
        if aa[i] > l:
            if ch == 0:
                ch += 1
                chg += 1
                a[i][1] = chg
            else:
                ch += 1
                a[i][1] = chg
        elif not ch != 0:
            ch = 0
    sol = list()
    if n > 1:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][1] == 0 and a[p][0] > l:
                    if p > 0 and p < n - 1:
                        if a[p - 1][1] == 0 and a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        elif a[p - 1][1] == 0 and a[p + 1][1] > 0:
                            a[p][1] = a[p + 1][1]
                        elif a[p - 1][1] > 0 and a[p + 1][1] == 0:
                            a[p][1] = a[p - 1][1]
                        else:
                            a[p][1] = a[p - 1][1]
                            chg -= 1
                    elif p == 0:
                        if a[p + 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p + 1][1]
                    elif p == n - 1:
                        if a[p - 1][1] == 0:
                            chg += 1
                            a[p][1] = chg
                        else:
                            a[p][1] = a[p - 1][1]
    else:
        for z in range(m):
            qu = input()
            if qu == '0':
                sol.append(chg)
            else:
                zz, p, d = list(map(int, qu.split()))
                p -= 1
                a[p][0] += d
                if a[p][0] > d:
                    chg = 1
    print(*sol, sep='\n')

def __starting_point():
    main()
__starting_point()

195

import math
from collections import Counter, defaultdict
from itertools import accumulate
R = lambda: map(int, input().split())
n = int(input())
spl = [math.inf]
ot_forbid = 0
sp = 0
res = 0
for i in range(n):
    arr = list(R())
    tp = arr[0]
    if tp == 1:
        sp = arr[1]
        while sp > spl[-1]:
            spl.pop()
            res += 1
    elif tp == 2:
        if ot_forbid > 0:
            res += ot_forbid
            ot_forbid = 0
    elif tp == -1:
        if arr[1] >= sp:
            spl.append(arr[1])
        else:
            res += 1
    elif tp == 4:
        ot_forbid = 0
    elif tp == 5:
        spl = [math.inf]
    else:
        ot_forbid += 1
print(res)

196

3

def build(n, s, t):
    ans = 1
    for i in range(n):
        if s[i] == '?' and t[i] == '?':
            ans = 55 * ans % (10 ** 9 + 7)
        elif s[i] == '?':
            ans = (ord(t[i]) - ord('0') + 1) * ans % (10 ** 9 + 7)
        elif t[i] == '?':
            ans = (ord('9') - ord(s[i]) + 1) * ans % (10 ** 9 + 7)
    return ans
n = int(input())
s = input()
t = input()
sltt = True
tlts = True
qm = 0
cqm = 0
for i in range(n):
    if t[i] == '?':
        qm += 1
    if s[i] == '?':
        qm += 1
    if t[i] == '?' and s[i] == '?':
        cqm += 1
    if t[i] == '?' or s[i] == '?':
        continue
    if ord(s[i]) < ord(t[i]):
        tlts = False
    if ord(t[i]) < ord(s[i]):
        sltt = False
if not sltt and (not tlts):
    print(pow(10, qm, 10 ** 9 + 7))
elif sltt and tlts:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, s, t) - build(n, t, s) + pow(10, cqm, 10 ** 9 + 7)) % (10 ** 9 + 7))
elif sltt:
    print(pow(10, qm, 10 ** 9 + 7) - build(n, s, t) & 10 ** 9 + 7)
else:
    print((pow(10, qm, 10 ** 9 + 7) - build(n, t, s)) % (10 ** 9 + 7))

197

MAX_SPEED = 300
n = int(input())
actions = [list(map(int, input().split())) for _ in range(n)]
speed = [0 for i in range(n)]
for i in range(n):
    if actions[i][0] == 1:
        lastSpeed = actions[i][1]
    speed[i] = lastSpeed
ignored = 0
overtook = False
maxSpeed = -1
for i in range(n - 1, -1, -1):
    maxSpeed = max(maxSpeed, speed[i])
    if actions[i][0] == 1:
        pass
    elif actions[i][0] == 4:
        overtook = True
    elif actions[i][0] == 3:
        if maxSpeed > actions[i][1]:
            ignored += 1
        else:
            maxSpeed = speed[i - 1]
    elif actions[i][0] == 4:
        overtook = False
    elif actions[i][0] == 5:
        maxSpeed = speed[i - 1]
    elif actions[i][0] == 6:
        if overtook:
            ignored += 1
print(ignored)

198

n, m, k = map(int, input().split())
col = list(map(int, input().split()))
p = []
for i in range(n):
    p.append(list(map(int, input().split())))
dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)]
if col[0] == 0:
    for i in range(m):
        dp[0][1][i] = p[0][i]
else:
    dp[0][1][col[0] - 1] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if j == 1:
            if not col[i]:
                dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1]
            else:
                for c in range(m):
                    dp[i][j][c] = dp[i - 1][j][c] + p[i][c]
        elif col[i]:
            x = dp[i - 1][j][col[i] - 1]
            for t in range(m):
                if t != col[i] - 1:
                    x = min(x, dp[i - 1][j - 1][t])
            dp[i][j][col[i] - 1] = x
        else:
            for c in range(m):
                x = dp[i - 1][j][c]
                for t in range(m):
                    if t != c:
                        x = min(x, dp[i - 1][j - 1][t])
                dp[i][j][c] = x + p[i][c]
ans = min(dp[-1][-1])
if ans != float('inf'):
    print(ans)
else:
    print(-1)

199

n, k = map(int, input().split())
a = list(map(int, input().split()))
d = list(map(int, input().split()))
if k == 0:
    best = 0
    curr = sum(a)
    for i in range(n):
        best = max(best, curr - d[i - 1])
        curr -= a[i]
    print(best)
elif k == 1:
    best = sum(a[:-1]) - min(d[:-1])
    other = sum(a)
    other -= sorted(d)[0]
    other -= sorted(d)[1]
    curr = sum(a)
    for i in range(n):
        if i:
            best = max(best, curr - d[i])
        curr -= a[i]
    o2 = sum(a) - min(a[1:]) - d[0]
    print(max((best, other, 0, o2)))
else:
    print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1])))

200

import sys
nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split()))
meches = list(map(int, sys.stdin.readline().split()))
trop_grand = [False] * nbmeches
pont = 0
for i in range(nbmeches):
    if meches[i] > l:
        trop_grand[i] = True
        if i == 0 or trop_grand[i - 1 - 1] is False:
            pont += 1
for i in range(nbreq):
    req = sys.stdin.readline().split()
    if int(req[0]) == 0:
        print(pont)
    else:
        meches[int(req[1]) - 1] += int(req[2])
        if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]):
            trop_grand[int(req[1]) - 1] = True
            if nbmeches == 1:
                pont = 1
            elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]):
                pont += 1
            elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]):
                pont += 1
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]:
                pont = max(0, pont - 1)
            elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]):
                pont += 1

Final Query

Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis.