// 演算法：用一個累加變數 cur 把 gain 變成「目前海拔」，best 追蹤最高海拔。
// Algorithm: accumulate gain into cur (current altitude); best tracks the maximum.
// O(n) time, O(1) extra space.

class Solution {
public:
    int largestAltitude(vector<int>& gain) {
        // cur：目前點的海拔，從起點 0 開始。
        // cur: current point's altitude, starting at 0.
        int cur = 0;

        // best：目前見過的最高海拔，從 0 開始（起點海拔為 0）。
        // best: highest altitude so far, starting at 0 (the start is altitude 0).
        int best = 0;

        // range-for 迴圈：依序把 gain 裡的每個值取出來叫做 g（不需要用索引）。
        // Range-for loop: pull each value out of gain in order as g (no index needed).
        for (int g : gain) {
            // 把這段的淨海拔變化加到 cur，cur 就變成下一個點的海拔。
            // Add this segment's net change to cur; cur becomes the next point's altitude.
            cur += g;

            // std::max 回傳兩者中較大的那個，用它更新最高海拔紀錄。
            // std::max returns the larger of the two; use it to update the highest altitude.
            best = max(best, cur);
        }

        // 回傳最高海拔。
        // Return the highest altitude.
        return best;
    }
};
