NazmusAshrafi/apps_probs huggingface dataset:
def max_trip_people(n: int, m: int, k: int, friendships: List[Tuple[int, int]]) -> List[int]:
    """
    There are $n$ persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
    
    We want to plan a trip for every evening of $m$ days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:   Either this person does not go on the trip,  Or at least $k$ of his friends also go on the trip.
    
    Note that the friendship is not transitive. That is, if $a$ and $b$ are friends and $b$ and $c$ are friends, it does not necessarily imply that $a$ and $c$ are friends.
    
    For each day, find the maximum number of people that can go on the trip on that day.
    
    
    -----Input-----
    
    The first line contains three integers $n$, $m$, and $k$ ($2 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5$, $1 \le k < n$) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
    
    The $i$-th ($1 \leq i \leq m$) of the next $m$ lines contains two integers $x$ and $y$ ($1\leq x, y\leq n$, $x\ne y$), meaning that persons $x$ and $y$ become friends on the morning of day $i$. It is guaranteed that $x$ and $y$ were not friends before.
    
    
    -----Output-----
    
    Print exactly $m$ lines, where the $i$-th of them ($1\leq i\leq m$) contains the maximum number of people that can go on the trip on the evening of the day $i$.
    
    
    -----Examples-----
    Input
    4 4 2
    2 3
    1 2
    1 3
    1 4
    
    Output
    0
    0
    3
    3
    
    Input
    5 8 2
    2 1
    4 2
    5 4
    5 2
    4 3
    5 1
    4 1
    3 2
    
    Output
    0
    0
    0
    3
    3
    4
    4
    5
    
    Input
    5 7 2
    1 5
    3 2
    2 5
    3 4
    1 2
    5 3
    1 3
    
    Output
    0
    0
    0
    0
    3
    4
    4
    
    
    
    -----Note-----
    
    In the first example,   $1,2,3$ can go on day $3$ and $4$.
    
    In the second example,   $2,4,5$ can go on day $4$ and $5$.  $1,2,4,5$ can go on day $6$ and $7$.  $1,2,3,4,5$ can go on day $8$.
    
    In the third example,   $1,2,5$ can go on day $5$.  $1,2,3,5$ can go on day $6$ and $7$.
    """


# Tests

def check(candidate):
    assert candidate(4, 4, 2, [(2, 3), (1, 2), (1, 3), (1, 4)]) == [0, 0, 3, 3]
    assert candidate(5, 8, 2, [(2, 1), (4, 2), (5, 4), (5, 2), (4, 3), (5, 1), (4, 1), (3, 2)]) == [0, 0, 0, 3, 3, 4, 4, 5]
    assert candidate(5, 7, 2, [(1, 5), (3, 2), (2, 5), (3, 4), (1, 2), (5, 3), (1, 3)]) == [0, 0, 0, 0, 3, 4, 4]
    assert candidate(2, 1, 1, [(2, 1)]) == [2]
    assert candidate(16, 20, 2, [(10, 3), (5, 3), (10, 5), (12, 7), (7, 6), (9, 12), (9, 6), (1, 10), (11, 16), (11, 1), (16, 2), (10, 2), (14, 4), (15, 14), (4, 13), (13, 15), (1, 8), (7, 15), (1, 7), (8, 15)]) == [0, 0, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7, 11, 11, 11, 11, 15, 15, 15, 16]
check(max_trip_people)
def count_u_shaped_parabolas(n: int, points: List[Tuple[int, int]]) -> int:
    """
    Recently Vasya learned that, given two points with different $x$ coordinates, you can draw through them exactly one parabola with equation of type $y = x^2 + bx + c$, where $b$ and $c$ are reals. Let's call such a parabola an $U$-shaped one.
    
    Vasya drew several distinct points with integer coordinates on a plane and then drew an $U$-shaped parabola through each pair of the points that have different $x$ coordinates. The picture became somewhat messy, but Vasya still wants to count how many of the parabolas drawn don't have any drawn point inside their internal area. Help Vasya.
    
    The internal area of an $U$-shaped parabola is the part of the plane that lies strictly above the parabola when the $y$ axis is directed upwards.
    
    
    -----Input-----
    
    The first line contains a single integer $n$ ($1 \le n \le 100\,000$) — the number of points.
    
    The next $n$ lines describe the points, the $i$-th of them contains two integers $x_i$ and $y_i$ — the coordinates of the $i$-th point. It is guaranteed that all points are distinct and that the coordinates do not exceed $10^6$ by absolute value.
    
    
    -----Output-----
    
    In the only line print a single integer — the number of $U$-shaped parabolas that pass through at least two of the given points and do not contain any of the given points inside their internal area (excluding the parabola itself).
    
    
    -----Examples-----
    Input
    3
    -1 0
    0 2
    1 0
    
    Output
    2
    
    Input
    5
    1 0
    1 -1
    0 -1
    -1 0
    -1 -1
    
    Output
    1
    
    
    
    -----Note-----
    
    On the pictures below all $U$-shaped parabolas that pass through at least two given points are drawn for each of the examples. The $U$-shaped parabolas that do not have any given point inside their internal area are drawn in red.  [Image] The first example.
    
    [Image] The second example.
    """


# Tests

def check(candidate):
    assert count_u_shaped_parabolas(3, [(-1, 0), (0, 2), (1, 0)]) == 2
    assert count_u_shaped_parabolas(5, [(1, 0), (1, -1), (0, -1), (-1, 0), (-1, -1)]) == 1
    assert count_u_shaped_parabolas(1, [(-751115, -925948)]) == 0
check(count_u_shaped_parabolas)
def minimize_eating_time(n: int, k: int, a: List[int]) -> int:
    """
    There are some rabbits in Singapore Zoo. To feed them, Zookeeper bought $n$ carrots with lengths $a_1, a_2, a_3, \ldots, a_n$. However, rabbits are very fertile and multiply very quickly. Zookeeper now has $k$ rabbits and does not have enough carrots to feed all of them. To solve this problem, Zookeeper decided to cut the carrots into $k$ pieces. For some reason, all resulting carrot lengths must be positive integers.
    
    Big carrots are very difficult for rabbits to handle and eat, so the time needed to eat a carrot of size $x$ is $x^2$.
    
    Help Zookeeper split his carrots while minimizing the sum of time taken for rabbits to eat the carrots.
    
    
    -----Input-----
    
    The first line contains two integers $n$ and $k$ $(1 \leq n \leq k \leq 10^5)$: the initial number of carrots and the number of rabbits.
    
    The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(1 \leq a_i \leq 10^6)$: lengths of carrots.
    
    It is guaranteed that the sum of $a_i$ is at least $k$.
    
    
    -----Output-----
    
    Output one integer: the minimum sum of time taken for rabbits to eat carrots.
    
    
    -----Examples-----
    Input
    3 6
    5 3 1
    
    Output
    15
    
    Input
    1 4
    19
    
    Output
    91
    
    
    
    -----Note-----
    
    For the first test, the optimal sizes of carrots are $\{1,1,1,2,2,2\}$. The time taken is $1^2+1^2+1^2+2^2+2^2+2^2=15$
    
    For the second test, the optimal sizes of carrots are $\{4,5,5,5\}$. The time taken is $4^2+5^2+5^2+5^2=91$.
    """


# Tests

def check(candidate):
    assert minimize_eating_time(3, 6, [5, 3, 1]) == 15
    assert minimize_eating_time(1, 4, [19]) == 91
    assert minimize_eating_time(1, 3, [1000000]) == 333333333334
    assert minimize_eating_time(1, 1, [1]) == 1
    assert minimize_eating_time(10, 23, [343, 984, 238, 758983, 231, 74, 231, 548, 893, 543]) == 41149446942
    assert minimize_eating_time(20, 40, [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) == 40
    assert minimize_eating_time(29, 99047, [206580, 305496, 61753, 908376, 272137, 803885, 675070, 665109, 995787, 667887, 164508, 634877, 994427, 270698, 931765, 721679, 518973, 65009, 804367, 608526, 535640, 117656, 342804, 398273, 369209, 298745, 365459, 942772, 89584]) == 2192719703
    assert minimize_eating_time(54, 42164, [810471, 434523, 262846, 930807, 148016, 633714, 247313, 376546, 142288, 30094, 599543, 829013, 182512, 647950, 512266, 827248, 452285, 531124, 257259, 453752, 114536, 833190, 737596, 267349, 598567, 781294, 390500, 318098, 354290, 725051, 978831, 905185, 849542, 761886, 55532, 608148, 631077, 557070, 355245, 929381, 280340, 620004, 285066, 42159, 82460, 348896, 446782, 672690, 364747, 339938, 715721, 870099, 357424, 323761]) == 17049737221
    assert minimize_eating_time(12, 21223, [992192, 397069, 263753, 561788, 903539, 521894, 818097, 223467, 511651, 737418, 975119, 528954]) == 2604648091
check(minimize_eating_time)
def maximize_column_sums(n: int, w: int, arrays: List[List[int]]) -> List[int]:
    """
    You are given $n$ arrays that can have different sizes. You also have a table with $w$ columns and $n$ rows. The $i$-th array is placed horizontally in the $i$-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table.
    
    You need to find the maximum sum of the integers in the $j$-th column for each $j$ from $1$ to $w$ independently.
    
    [Image] Optimal placements for columns $1$, $2$ and $3$ are shown on the pictures from left to right.
    
    Note that you can exclude any array out of a column provided it remains in the window. In this case its value is considered to be zero.
    
    
    -----Input-----
    
    The first line contains two integers $n$ ($1 \le n \le 10^{6}$) and $w$ ($1 \le w \le 10^{6}$) — the number of arrays and the width of the table.
    
    Each of the next $n$ lines consists of an integer $l_{i}$ ($1 \le l_{i} \le w$), the length of the $i$-th array, followed by $l_{i}$ integers $a_{i1}, a_{i2}, \ldots, a_{il_i}$ ($-10^{9} \le a_{ij} \le 10^{9}$) — the elements of the array.
    
    The total length of the arrays does no exceed $10^{6}$.
    
    
    -----Output-----
    
    Print $w$ integers, the $i$-th of them should be the maximum sum for column $i$.
    
    
    -----Examples-----
    Input
    3 3
    3 2 4 8
    2 2 5
    2 6 3
    
    Output
    10 15 16
    
    Input
    2 2
    2 7 8
    1 -8
    
    Output
    7 8
    
    
    
    -----Note-----
    
    Illustration for the first example is in the statement.
    """


# Tests

def check(candidate):
    assert candidate(3, 3, [[3, 2, 4, 8], [2, 2, 5], [2, 6, 3]]) == [10, 15, 16]
    assert candidate(2, 2, [[2, 7, 8], [1, -8]]) == [7, 8]
check(maximize_column_sums)
def maximize_haybales(t: int, test_cases: List[Tuple[int, int, List[int]]]) -> List[int]:
    """
    The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of $n$ haybale piles on the farm. The $i$-th pile contains $a_i$ haybales.
    
    However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices $i$ and $j$ ($1 \le i, j \le n$) such that $|i-j|=1$ and $a_i>0$ and apply $a_i = a_i - 1$, $a_j = a_j + 1$. She may also decide to not do anything on some days because she is lazy.
    
    Bessie wants to maximize the number of haybales in pile $1$ (i.e. to maximize $a_1$), and she only has $d$ days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile $1$ if she acts optimally!
    
    
    -----Input-----
    
    The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 100$)  — the number of test cases. Next $2t$ lines contain a description of test cases  — two lines per test case.
    
    The first line of each test case contains integers $n$ and $d$ ($1 \le n,d \le 100$) — the number of haybale piles and the number of days, respectively.
    
    The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 100$)  — the number of haybales in each pile.
    
    
    -----Output-----
    
    For each test case, output one integer: the maximum number of haybales that may be in pile $1$ after $d$ days if Bessie acts optimally.
    
    
    -----Example-----
    Input
    3
    4 5
    1 0 3 2
    2 2
    100 1
    1 8
    0
    
    Output
    3
    101
    0
    
    
    
    -----Note-----
    
    In the first test case of the sample, this is one possible way Bessie can end up with $3$ haybales in pile $1$:   On day one, move a haybale from pile $3$ to pile $2$  On day two, move a haybale from pile $3$ to pile $2$  On day three, move a haybale from pile $2$ to pile $1$  On day four, move a haybale from pile $2$ to pile $1$  On day five, do nothing
    
    In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile $2$ to pile $1$ on the second day.
    """


# Tests

def check(candidate):
    assert candidate(3, [(4, 5, [1, 0, 3, 2]), (2, 2, [100, 1]), (1, 8, [0])]) == [3, 101, 0]
check(maximize_haybales)
def weight_of_array(n: int, q: int, a: List[int], queries: List[Tuple[int, int]]) -> List[int]:
    """
    Let $a_1, \ldots, a_n$ be an array of $n$ positive integers. In one operation, you can choose an index $i$ such that $a_i = i$, and remove $a_i$ from the array (after the removal, the remaining parts are concatenated).
    
    The weight of $a$ is defined as the maximum number of elements you can remove.
    
    You must answer $q$ independent queries $(x, y)$: after replacing the $x$ first elements of $a$ and the $y$ last elements of $a$ by $n+1$ (making them impossible to remove), what would be the weight of $a$?
    
    
    -----Input-----
    
    The first line contains two integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$)  — the length of the array and the number of queries.
    
    The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \leq a_i \leq n$) — elements of the array.
    
    The $i$-th of the next $q$ lines contains two integers $x$ and $y$ ($x, y \ge 0$ and $x+y < n$).
    
    
    -----Output-----
    
    Print $q$ lines, $i$-th line should contain a single integer  — the answer to the $i$-th query.
    
    
    -----Examples-----
    Input
    13 5
    2 2 3 9 5 4 6 5 7 8 3 11 13
    3 1
    0 0
    2 4
    5 0
    0 12
    
    Output
    5
    11
    6
    1
    0
    
    Input
    5 2
    1 4 1 2 4
    0 0
    1 0
    
    Output
    2
    0
    
    
    
    -----Note-----
    
    Explanation of the first query:
    
    After making first $x = 3$ and last $y = 1$ elements impossible to remove, $a$ becomes $[\times, \times, \times, 9, 5, 4, 6, 5, 7, 8, 3, 11, \times]$ (we represent $14$ as $\times$ for clarity).
    
    Here is a strategy that removes $5$ elements (the element removed is colored in red):  $[\times, \times, \times, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, \times]$  $[\times, \times, \times, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, \times]$  $[\times, \times, \times, 9, 4, \color{red}{6}, 5, 7, 8, 3, \times]$  $[\times, \times, \times, 9, 4, 5, 7, \color{red}{8}, 3, \times]$  $[\times, \times, \times, 9, 4, 5, \color{red}{7}, 3, \times]$  $[\times, \times, \times, 9, 4, 5, 3, \times]$ (final state)
    
    It is impossible to remove more than $5$ elements, hence the weight is $5$.
    """


# Tests

def check(candidate):
    assert candidate(13, 5, [2, 2, 3, 9, 5, 4, 6, 5, 7, 8, 3, 11, 13], [(3, 1), (0, 0), (2, 4), (5, 0), (0, 12)]) == [5, 11, 6, 1, 0]
    assert candidate(5, 2, [1, 4, 1, 2, 4], [(0, 0), (1, 0)]) == [2, 0]
    assert candidate(1, 1, [1], [(0, 0)]) == [1]
    assert candidate(30, 10, [1, 1, 3, 3, 5, 2, 1, 8, 2, 6, 11, 5, 2, 6, 12, 11, 8, 5, 11, 3, 14, 8, 16, 13, 14, 25, 16, 2, 8, 17], [(6, 3), (0, 15), (1, 0), (9, 2), (12, 16), (1, 0), (17, 3), (14, 13), (0, 22), (3, 10)]) == [3, 15, 16, 2, 0, 16, 0, 0, 8, 4]
check(weight_of_array)