Skip to content

Instantly share code, notes, and snippets.

@LplusKira
Created July 10, 2022 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LplusKira/332d4342d7e401ecfe0121395ba981bc to your computer and use it in GitHub Desktop.
Save LplusKira/332d4342d7e401ecfe0121395ba981bc to your computer and use it in GitHub Desktop.
#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