인덱스를 기준으로 i번째 사람의 나이와 점수를 뜻하는 정수형 벡터 두 개를 받는다.
그리고 팀을 꾸리기 위하여 사람을 뽑는데,
나이가 많은 사람이 무조건 나이가 적은 사람보다 점수가 높아야 한다는 조건이 있다.
문제는 조건을 만족하는 팀을 꾸려 팀 인원들의 점수를 합했을 때 최대값을 구하는 것이다.
class Solution {
public:
int bestTeamScore(vector<int>& scores, vector<int>& ages) {
vector<pair<int, int>> sortedAge{};
int length = scores.size();
for (int i = 0; i < length; ++i)
{
sortedAge.push_back({ages[i], scores[i]});
}
std::sort(sortedAge.begin(), sortedAge.end());
vector<int> memo(length);
int result{0};
for (int i = 0; i < length; ++i)
{
memo[i] = sortedAge[i].second;
for (int j = 0; j < i; ++j)
{
if (sortedAge[j].second <= sortedAge[i].second)
{
memo[i] = std::max(memo[i], memo[j] + sortedAge[i].second);
}
}
result = std::max(result, memo[i]);
}
return result;
}
};