// 演算法 / Algorithm:
// 與 C 版相同：對每個元素計算數字和（反覆 %10 與 /10），
// 用 range-for 遍歷並用 min 維護最小值。
// Same as the C version: digit sum per element, tracked with a running min.

#include <vector>      // std::vector 動態陣列 / dynamic array container
#include <algorithm>   // std::min
#include <climits>     // INT_MAX
using namespace std;

class Solution {
public:
    int minElement(vector<int>& nums) {
        int ans = INT_MAX;             // 初始設為最大值 / start at the largest int

        for (int x : nums) {           // range-for：x 依序取得每個元素的值 / iterate values directly
            int n = x;                 // 可修改的副本，避免改動原始資料 / mutable copy
            int sum = 0;               // 這個數字的數字和 / digit sum for this number

            while (n > 0) {            // 逐位處理直到 n 變 0 / process each digit until none left
                sum += n % 10;         // 取個位並累加 / add last digit
                n /= 10;               // 去掉個位 / remove last digit
            }

            ans = min(ans, sum);       // std::min 取兩者較小者 / keep the smaller of the two
        }

        return ans;                    // 回傳結果 / return the answer
    }
};
