Prefix Sum
The energy is simple - we just need the sum of energy of all opponents, plus one.\n\nThe experience is trickier, as it depends on the order of opponents.\n\nC++
The energy is simple - we just need the sum of energy of all opponents, plus one.\n\nThe experience is trickier, as it depends on the order of opponents.\n\n**C++**\n```cpp\nint minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {\n    int need_exp = 0, cur_exp = initialExperience;\n    for (int i = 0; i < experience.size();  cur_exp += experience[i++])\n        need_exp += max(experience[i] - need_exp - cur_exp + 1, 0);\n    return max(accumulate(begin(energy), end(energy), 1) - initialEnergy, 0) + need_exp;\n}\n```\n\nNo raw loops version, just for fun. It\'s neither shorter nor more efficient, just wanted to see if it can be done.\n\nWe use prefix sum (partial sum) and transform to compute additional experience we need for each opponent.\n\n**C++**\n```cpp\nint minNumberOfHours(int initialEnergy, int initialExp, vector<int>& energy, vector<int>& exp) {\n    vector<int> ps;\n    partial_sum(begin(exp), end(exp), back_inserter(ps));\n    transform(begin(ps) + 1, end(ps), begin(ps), begin(exp) + 1, [](int pa, int pb) { return max(pa - 2 * pb, 0); });\n    return max(accumulate(begin(energy), end(energy), 1) - initialEnergy, 0) + \n        max(accumulate(begin(exp), end(exp), 0, [](int need, int delta) { return need + max(delta - need + 1, 0); }) - initialExp, 0);\n}\n```
['C']