classSolution{public:intmaxSumDivThree(vector<int>&nums){vector<int>dp(3);// dp[i] := the maximum sum so far s.t. sum % 3 == ifor(constintnum:nums)for(constintsum:vector<int>(dp))dp[(sum+num)%3]=max(dp[(sum+num)%3],sum+num);returndp[0];}};
1 2 3 4 5 6 7 8 91011
classSolution{publicintmaxSumDivThree(int[]nums){int[]dp=newint[3];// dp[i] := the maximum sum so far s.t. sum % 3 == ifor(finalintnum:nums)for(finalintsum:dp.clone())dp[(sum+num)%3]=Math.max(dp[(sum+num)%3],sum+num);returndp[0];}}