1262.cpp (379B)
1 class Solution { 2 public: 3 int maxSumDivThree(const vector<int> &nums) const { 4 int dp[3] = {0}, prev[3]; 5 6 for (const int n : nums) { 7 memcpy(prev, dp, 3 * sizeof(int)); 8 for (const int prev : span(prev, 3)) { 9 dp[(prev + n) % 3] = max(dp[(prev + n) % 3], prev + n); 10 } 11 } 12 13 return dp[0]; 14 } 15 };