[Python] Heapq
Buy list order by negative price so that it can be from greater to smaller.\n\npython\n    def getNumberOfBacklogOrders(self, orders):\n        """\n        :ty
Buy list order by negative price so that it can be from greater to smaller.\n\n```python\n    def getNumberOfBacklogOrders(self, orders):\n        """\n        :type orders: List[List[int]]\n        :rtype: int\n        """\n        import heapq\n        sells = []\n        buys = []\n        for p,a,t in orders:\n            if t == 0:\n                while sells:\n                    p_, a_ = heapq.heappop(sells)\n                    if p_ <= p:\n                        if a >= a_:\n                            a -= a_\n                        else:\n                            heapq.heappush(sells, (p_, a_-a))\n                            a = 0\n                            break\n                    else:\n                        heapq.heappush(sells, (p_, a_))\n                        break\n                if a:\n                    heapq.heappush(buys, (-p, a))\n            else:\n                while buys:\n                    p_, a_ = heapq.heappop(buys)\n                    p_ = -p_\n                    if p_ >= p:\n                        if a >= a_:\n                            a -= a_\n                        else:\n                            heapq.heappush(buys, (-p_, a_-a))\n                            a = 0\n                            break\n                    else:\n                        heapq.heappush(buys, (-p_, a_))\n                        break\n                if a:\n                    heapq.heappush(sells, (p, a))\n        return (sum(x[1] for x in sells) + sum(x[1] for x in buys)) % (10**9+7)\n```
['Python']