动态规划
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
using namespace::std;
using std::cout;
using std::cin;
int zsyx(vector<int>& nums)
{
vector<int> v[3];
for (int num: nums)
{
v[num % 3].push_back(num);
}
sort(v[1].begin(), v[1].end(), greater<int>());
sort(v[2].begin(), v[2].end(), greater<int>());
int ans = 0;
int lb = v[1].size(), lc = v[2].size();
for (int cntb = lb - 2; cntb <= lb; ++cntb)
{
if (cntb >= 0)
{
for (int cntc = lc - 2; cntc <= lc; ++cntc)
{
if (cntc >= 0 && (cntb - cntc) % 3 == 0)
{
ans = max(ans, accumulate(v[1].begin(), v[1].begin() + cntb, 0) + accumulate(v[2].begin(), v[2].begin() + cntc, 0));
}
}
}
}
return ans + accumulate(v[0].begin(), v[0].end(), 0);
}
int main()
{
string s;
cin >> s;
int start=0;
int num=0;
vector<int> nums;
for (int i=0; i<s.size(); i++)
{
if (s[i] == ',')
{
string temp = s.substr(start, i-start);
nums.push_back(atoi(temp.c_str()));
num++;
start = i+1;
}
}
string temp = s.substr(start, s.size()-start+1);
nums.push_back(atoi(temp.c_str()));
std::cout << zsyx(nums);
return 0;
}