The i-th person has weight people[i], and each boat can carry a maximum weight of limit.
Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit.
Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)
 

Example 1:
Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)


Example 2:
Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)


Example 3:
Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)
Note:

1 <= people.length <= 50000
1 <= people[i] <= limit <= 30000

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

def _disabled_input(*args, **kwargs):
    raise RuntimeError("input() is disabled in callable mode")
builtins.input = _disabled_input

ns = {"__name__": "__judge__"}  # prevent if __name__ == "__main__" blocks
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = 'class Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        less_than_half = dict()\n        more_than_half = dict()\n        half = 0\n        total_boats = 0\n        for person in people:\n            diff = limit - person\n            if diff == 0:\n                total_boats += 1\n                continue\n            if person < limit/2:\n                if diff in more_than_half:\n                    total_boats += 1\n                    if more_than_half[diff] == 1:\n                        del more_than_half[diff]\n                    else:\n                        more_than_half[diff] -= 1\n                else:\n                    if person in less_than_half:\n                        less_than_half[person] += 1\n                    else:\n                        less_than_half[person] = 1\n            elif person > limit/2:\n                if diff in less_than_half:\n                    total_boats += 1\n                    if less_than_half[diff] == 1:\n                        del less_than_half[diff]\n                    else:\n                        less_than_half[diff] -= 1\n                else:\n                    if person in more_than_half:\n                        more_than_half[person] += 1\n                    else:\n                        more_than_half[person] = 1\n            else:\n                if half == 1:\n                    total_boats += 1\n                    half = 0\n                else:\n                    half = 1\n        \n        less_keys = sorted(less_than_half.keys())\n        more_keys = sorted(list(more_than_half.keys()), reverse=True)\n        \n        while len(less_keys) and len(more_keys):\n            if less_keys[0] + more_keys[0] <= limit:\n                if less_than_half[less_keys[0]] < more_than_half[more_keys[0]]:\n                    total_boats += less_than_half[less_keys[0]]\n                    more_than_half[more_keys[0]] -= less_than_half[less_keys[0]]\n                    less_keys.pop(0)\n                elif less_than_half[less_keys[0]] > more_than_half[more_keys[0]]:\n                    total_boats += more_than_half[more_keys[0]]\n                    less_than_half[less_keys[0]] -= more_than_half[more_keys[0]]\n                    more_keys.pop(0)\n                else:\n                    total_boats += less_than_half[less_keys[0]]\n                    less_keys.pop(0)\n                    more_keys.pop(0)\n            else:\n                total_boats += more_than_half[more_keys[0]]\n                more_keys.pop(0)\n                    \n        less_total = 0\n        for k in less_keys:\n            less_total += less_than_half[k]\n        \n        more_total = 0\n        for k in more_keys:\n            more_total += more_than_half[k]\n        \n        # we can pair up each of the less than half weights\n        total_boats += (less_total+half + 1) // 2 + more_total\n        \n        return total_boats\n\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

_target = None
_fn_name = 'numRescueBoats'
if _fn_name:
    if "Solution" in ns:
        try:
            _obj = ns["Solution"]()
            if hasattr(_obj, _fn_name):
                _target = getattr(_obj, _fn_name)
        except Exception:
            _target = None
    if _target is None and _fn_name in ns and callable(ns[_fn_name]):
        _target = ns[_fn_name]
# Fallback: if there's exactly one user-defined function, use it
if _target is None:
    _cands = [v for k, v in ns.items() if callable(v) and getattr(v, "__module__", "") in ("__judge__", "__main__")]
    _user_funcs = [c for c in _cands if getattr(c, "__name__", "").startswith("_") is False]
    if len(_user_funcs) == 1:
        _target = _user_funcs[0]
if _target is None:
    raise RuntimeError(f"Could not resolve callable '{_fn_name}'")

_raw = sys.stdin.read().strip()
if _raw:
    _parsed = json.loads(_raw)
    if isinstance(_parsed, dict):
        _kwargs = _parsed.get("kwargs", {})
        _args = _parsed.get("args", [])
    else:
        _args, _kwargs = _parsed, {}
else:
    _args, _kwargs = [], {}
if not isinstance(_args, (list, tuple)):
    _args = [_args]
_res = _target(*_args, **_kwargs)
try:
    sys.stdout.write(repr(_res) + "\n")
except Exception:
    pass

```

Measured memory_bytes: 7028