#!/usr/bin/env python3 from heapq import heappush, heappop def update(segtree, pos, value): # change the index to leaf node first pos += len(segtree) // 2 # update the value at the leaf node at the exact index segtree[pos] = value while pos > 1: # move up one level at a time in the tree pos >>= 1 # update the values in the nodes in the next higher level segtree[pos] = segtree[2 * pos] + segtree[2 * pos + 1] def range_query(segtree, left, right): # change the index to leaf node first left += len(segtree) // 2 right += len(segtree) // 2 ans = 0 while left < right: # if left index is odd if (left & 1) == 1: ans += segtree[left] # make left index even left += 1 # if right index is odd if (right & 1) == 1: # make right index even right -= 1 ans += segtree[right] # move to the next higher level left >>= 1 right >>= 1 return ans n = int(input()) buy_tree, sell_tree = [0] * (2 * n), [0] * (2 * n) buys, sells = [], [] orders = [(a == "buy", b == "fok", int(c), int(d)) for a, b, c, d in [input().split() for _ in range(n)]] buy_index = {j: i for i, (j, _) in enumerate(sorted(list(enumerate(orders)), key=lambda p: (p[1][2], -p[0])))} sell_index = {j: i for i, (j, _) in enumerate(sorted(list(enumerate(orders)), key=lambda p: (p[1][2], p[0])))} answers = [] for i, (buy, fok, p, a) in enumerate(orders): if buy: remaining = range_query(sell_tree, 0, sell_index[i]) if not fok or remaining >= a: while remaining and sells and a: sell_p, sell_i, sell_a = heappop(sells) amount = min(a, sell_a, remaining) update(sell_tree, sell_index[sell_i], sell_a - amount) if sell_a - amount > 0: heappush(sells, (sell_p, sell_i, sell_a - amount)) remaining -= amount a -= amount answers.append((sell_i + 1, i + 1, amount)) if a > 0: update(buy_tree, buy_index[i], a) heappush(buys, (-p, i, a)) # Highest price first, so negate price else: remaining = range_query(buy_tree, buy_index[i], n) if not fok or remaining >= a: while remaining and buys and a: buy_p, buy_i, buy_a = heappop(buys) amount = min(a, buy_a, remaining) update(buy_tree, buy_index[buy_i], buy_a - amount) if buy_a - amount > 0: heappush(buys, (buy_p, buy_i, buy_a - amount)) remaining -= amount a -= amount answers.append((i + 1, buy_i + 1, amount)) if a > 0: update(sell_tree, sell_index[i], a) heappush(sells, (p, i, a)) # Lowest price first print(len(answers)) for answer in answers: print(*answer)