856. Score of Parentheses

class Solution {
public:
    int scoreOfParentheses(string s) {
        int res = 0;
        stack<int> stk;
        for (auto c : s) {
            if(c == '(') {
                stk.push(res);
                res = 0;
            } else {
                if (res == 0) {
                    res = 1 + stk.top();
                } else {
                    res = res * 2 + stk.top();
                }
                stk.pop();
            }
        }
        return res;
    }
};