// 演算法：小行星由小到大排序後依序碰撞，貪心地先吃最小的。
// Algorithm: sort ascending, then greedily collide smallest-first.
// 累加用 long long 防溢位；任何一顆撞不過就回傳 false。
// Accumulate in long long to avoid overflow; any failed collision → false.

class Solution {
public:
    bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
        // std::sort 預設由小到大排序 vector / std::sort orders the vector ascending by default.
        // 參數是「起點迭代器」和「終點迭代器」/ takes begin() and end() iterators.
        sort(asteroids.begin(), asteroids.end());

        // cur 用 long long 避免質量總和超過 int 範圍 / long long avoids int overflow on the running sum.
        long long cur = mass;

        // range-for：依序取出每顆小行星的質量 a / range-for iterates each asteroid's mass a.
        // 用 const int& 避免複製、且表明不修改 / const int& avoids a copy and signals no mutation.
        for (const int& a : asteroids) {
            // 若當前質量撞不過這顆（最小的剩餘）小行星，直接失敗。
            // If current mass can't beat this (smallest remaining) asteroid, fail immediately.
            if (cur < a) return false;
            // 否則吸收它的質量 / otherwise absorb its mass.
            cur += a;
        }

        // 全部成功摧毀 / all destroyed successfully.
        return true;
    }
};
