Created
July 10, 2022 22:43
-
-
Save LplusKira/332d4342d7e401ecfe0121395ba981bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <cassert> | |
using namespace std; | |
class Solver { | |
public: | |
Solver() = default; | |
int solve(vector<int>& pkgs) { | |
if (pkgs.empty()) return 0; | |
int n = pkgs.size(); | |
int curMax = pkgs[n - 1]; | |
for (int i = n - 2; i >= 0; i--) { | |
if (pkgs[i] >= curMax) { | |
curMax = pkgs[i]; | |
} else { | |
curMax += pkgs[i]; | |
} | |
} | |
return curMax; | |
} | |
}; | |
void tests() { | |
{ | |
Solver s; | |
vector<int> pkgs = {2, 9, 10, 3, 7}; | |
assert(s.solve(pkgs) == 21); | |
} | |
{ | |
Solver s; | |
vector<int> pkgs = {2, 9, 11, 3, 7}; | |
assert(s.solve(pkgs) == 22); | |
} | |
{ | |
Solver s; | |
vector<int> pkgs = {2, 3, 7}; | |
assert(s.solve(pkgs) == 12); | |
} | |
} | |
int main() { | |
tests(); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment