// 演算法 / Algorithm: 與 C 版相同的後綴 DP（博弈 minimax）。
// dp[i] = 當前玩家面對石頭 i..n-1 時保證的最佳「分數差」(自己 − 對手)。
// dp[i] = max_{k=1..3}( 前 k 顆和 − dp[i+k] )，dp[n]=0；由 dp[0] 的正負判定勝負。
// Same suffix DP as the C version: dp[0]'s sign decides the winner.

#include <string>       // std::string — 回傳型別 / return type
#include <vector>       // std::vector — 動態陣列，會自動管理記憶體 / auto-managing dynamic array
#include <algorithm>    // std::max — 取兩數較大者 / picks the larger of two values
#include <climits>      // LONG_MIN — 起始哨兵值 / sentinel starting value
using namespace std;

class Solution {
public:
    string stoneGameIII(vector<int>& stoneValue) {
        int n = stoneValue.size();                 // n = 石頭數量 / number of stones

        // vector<long> 建立 n+1 個元素、全部初始化為 0；dp[n]=0 即為邊界。
        // vector<long> of size n+1, all zero-initialized — dp[n]=0 is already our base case.
        // vector 相比 malloc 的好處：離開作用域自動釋放，不必手動 free。
        // Unlike malloc, a vector frees itself when it goes out of scope — no manual free.
        vector<long> dp(n + 1, 0);

        // 由後往前：算 dp[i] 時，右邊的 dp 都已算好。
        // Right-to-left: when computing dp[i], everything to its right is ready.
        for (int i = n - 1; i >= 0; --i) {
            long take = 0;                         // 本步累計取走的和 / running sum taken this move
            long best = LONG_MIN;                  // 目前最佳差值 / best difference so far

            // k=0,1,2 → 取 1,2,3 顆；i+k<n 防止越界。
            // k=0,1,2 → take 1,2,3 stones; i+k<n keeps us in bounds.
            for (int k = 0; k < 3 && i + k < n; ++k) {
                take += stoneValue[i + k];         // 加入第 (i+k) 顆 / add stone (i+k)
                // std::max 比較「本步兩個候選差值」，留較大者。
                // std::max keeps the larger of the current best and this new option.
                best = max(best, take - dp[i + k + 1]);
            }
            dp[i] = best;                          // 存下後綴 i 的最佳差值 / store best difference for suffix i
        }

        long res = dp[0];                          // 整場遊戲 Alice 的最佳差值 / Alice's best overall difference
        // 用三元/條件回傳字串；差值正負對應勝負。
        // Return the result string based on the sign of the difference.
        if (res > 0) return "Alice";
        if (res < 0) return "Bob";
        return "Tie";
    }
};
