/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 14ms 3.16 MiB
#2 Wrong Answer 17ms 3.41 MiB

Code

import sys
import heapq

def solve():
    """
    Solves a single test case for the maximum profit problem.
    """
    try:
        n = int(sys.stdin.readline())
        a = list(map(int, sys.stdin.readline().split()))
        b = list(map(int, sys.stdin.readline().split()))
    except (IOError, ValueError):
        return

    # A min-priority queue to store the costs of items bought but not yet sold
    held_items_costs = []
    total_profit = 0

    for i in range(n):
        # On day i, the option to buy at price a[i] becomes available.
        # An item bought today can be sold today. 
        heapq.heappush(held_items_costs, a[i])

        # See if we can make profitable sales with today's selling price b[i].
        # We can sell multiple items.
        while held_items_costs and b[i] > held_items_costs[0]:
            # It's profitable to sell the cheapest item we hold.
            cheapest_cost = heapq.heappop(held_items_costs)
            total_profit += b[i] - cheapest_cost

    print(total_profit)

def main():
    """
    Main function to handle multiple test cases.
    """
    try:
        num_test_cases = int(sys.stdin.readline())
        for _ in range(num_test_cases):
            solve()
    except (IOError, ValueError):
        pass

if __name__ == "__main__":
    main()

Information

Submit By
Type
Submission
Problem
P1228 Business Strategy
Contest
Testing - Intra LU Programming contest 25
Language
Python 3 (Python 3.12.3)
Submit At
2025-08-30 20:10:20
Judged At
2025-08-30 20:10:20
Judged By
Score
0
Total Time
17ms
Peak Memory
3.41 MiB