This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "happiness.h"
using namespace std;
struct Node
{
    Node *left, *right;
    int64_t sum, min_prefix_sum;
};
Node *root;
int64_t l = 1;
void check_children(Node *const x)
{
    if (!x->left)
    {
        x->left = (Node *)calloc(1, sizeof *x->left);
        x->left->min_prefix_sum = INT64_MAX / 2;
    }
    if (!x->right)
    {
        x->right = (Node *)calloc(1, sizeof *x->right);
        x->right->min_prefix_sum = INT64_MAX / 2;
    }
}
void combine(Node *const x)
{
    x->sum = x->left->sum + x->right->sum;
    x->min_prefix_sum = min(x->left->min_prefix_sum,
                            x->left->sum + x->right->min_prefix_sum);
}
void update(int64_t i, Node *const x, int64_t a, int64_t b, int64_t t)
{
    if (a + 1 == b)
    {
        x->sum += t * i;
        x->min_prefix_sum = x->sum ? -i + 1 : INT64_MAX / 2;
        return;
    }
    check_children(x);
    if (i <= (a + b) >> 1)
        update(i, x->left, a, (a + b) >> 1, t);
    else
        update(i, x->right, (a + b) >> 1, b, t);
    combine(x);
}
bool init(int n, long long m, long long coins[])
{
    while (l < m)
        l <<= 1;
    root = (Node *)calloc(1, sizeof *root);
    for (size_t i = 0; i < n; i++)
        update(coins[i], root, 0, l, 1);
    return root->min_prefix_sum >= 0;
}
bool is_happy(int event, int n, long long coins[])
{
    for (size_t i = 0; i < n; i++)
        update(coins[i], root, 0, l, event);
    return root->min_prefix_sum >= 0;
}
Compilation message (stderr)
happiness.cpp: In function 'bool init(int, long long int, long long int*)':
happiness.cpp:57:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   57 |     for (size_t i = 0; i < n; i++)
      |                        ~~^~~
happiness.cpp: In function 'bool is_happy(int, int, long long int*)':
happiness.cpp:64:26: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   64 |     for (size_t i = 0; i < n; i++)
      |                        ~~^~~
grader.cpp: In function 'int main()':
grader.cpp:16:12: warning: unused variable 'max_code' [-Wunused-variable]
   16 |  long long max_code;
      |            ^~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |