Bitmask dp
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minSessions(vector<int>& tasks, int sessionTime) {\n        const int inf = 2e9;\n        int n = tasks.size();\n        vector<int> dp(1<<n, inf); // key: mask, value: minimum total time ( complete session * sessionTime + remainder)\n\n        dp[0] = 0;\n        for (int i = 1 ; i < (1<<n) ; ++i) {\n            for (int j = 0 ; j < n ; ++j) {\n                if ((i>>j)&1) {\n                    int prev_mask = i^(1<<j); // guaranteed < i, hence value already decided\n\n                    int complete_sessions = dp[prev_mask] / sessionTime;\n                    int remaining_usage = dp[prev_mask] % sessionTime;\n\n                    if (remaining_usage + tasks[j] <= sessionTime) {\n                        dp[i] = min(dp[i], dp[prev_mask] + tasks[j]);\n                    }\n\n                    else {\n                        dp[i] = min(dp[i], (complete_sessions + 1) * sessionTime + tasks[j]);\n                    }\n\n                }\n            }\n        }\n\n        if (dp.back() % sessionTime == 0) return dp.back() / sessionTime;\n        return dp.back() / sessionTime + 1;\n    }\n};\n```\n\n```cpp []\nclass Solution {\npublic:\n    typedef pair<int,int> p;\n\n    vector<p> dp;\n\n    p solve(vector<int>& tasks, int sessionTime, int mask) { // output: first = sessions used, second = last session time\n        int n = tasks.size();\n        if (dp[mask].first != n + 1) { // we already calculated this\n            return dp[mask];\n        }\n\n        for (int bit = 0 ; bit < n ; ++bit) {\n            if ((mask>>bit)&1) {\n                int prev_mask = mask^(1<<bit);\n                p prev_value = solve(tasks, sessionTime, prev_mask);\n                if (prev_value.second + tasks[bit] <= sessionTime) {\n                    prev_value.second += tasks[bit];\n                    dp[mask] = min(dp[mask], prev_value);\n                }\n                else {\n                    prev_value.first++;\n                    prev_value.second = tasks[bit];\n                    dp[mask] = min(dp[mask], prev_value);\n                }\n            }\n        }\n\n        return dp[mask];\n\n    }\n\n    int minSessions(vector<int>& tasks, int sessionTime) {\n        int n = tasks.size();\n\n        // initialize dp\n        dp = vector<p>(1<<n, make_pair(n + 1, 0));\n        dp[0] = make_pair(1, 0); // it\'s using 1 session, and we use up 0 seconds in the last session \n        return solve(tasks, sessionTime, (1<<n) - 1).first;\n    }\n};\n```
['C++']