classSolution{public:vector<int>maxSumOfThreeSubarrays(vector<int>&nums,intk){constintn=nums.size()-k+1;vector<int>sums(n);// sums[i] := sum of nums[i..i + k)vector<int>l(n);// l[i] := index in [0..i] having max sums[i]vector<int>r(n);// r[i] := index in [i..n) having max sums[i]intsum=0;for(inti=0;i<nums.size();++i){sum+=nums[i];if(i>=k)sum-=nums[i-k];if(i>=k-1)sums[i-k+1]=sum;}intmaxIndex=0;for(inti=0;i<n;++i){if(sums[i]>sums[maxIndex])maxIndex=i;l[i]=maxIndex;}maxIndex=n-1;for(inti=n-1;i>=0;--i){if(sums[i]>=sums[maxIndex])maxIndex=i;r[i]=maxIndex;}vector<int>ans{-1,-1,-1};for(inti=k;i<n-k;++i)if(ans[0]==-1||sums[ans[0]]+sums[ans[1]]+sums[ans[2]]<sums[l[i-k]]+sums[i]+sums[r[i+k]]){ans[0]=l[i-k];ans[1]=i;ans[2]=r[i+k];}returnans;}};
classSolution{publicint[]maxSumOfThreeSubarrays(int[]nums,intk){finalintn=nums.length-k+1;int[]sums=newint[n];// sums[i] := sum of nums[i..i + k)int[]l=newint[n];// l[i] := index in [0..i] having max sums[i]int[]r=newint[n];// r[i] := index in [i..n) having max sums[i]intsum=0;for(inti=0;i<nums.length;++i){sum+=nums[i];if(i>=k)sum-=nums[i-k];if(i>=k-1)sums[i-k+1]=sum;}intmaxIndex=0;for(inti=0;i<n;++i){if(sums[i]>sums[maxIndex])maxIndex=i;l[i]=maxIndex;}maxIndex=n-1;for(inti=n-1;i>=0;--i){if(sums[i]>=sums[maxIndex])maxIndex=i;r[i]=maxIndex;}int[]ans={-1,-1,-1};for(inti=k;i+k<n;++i)if(ans[0]==-1||sums[ans[0]]+sums[ans[1]]+sums[ans[2]]<sums[l[i-k]]+sums[i]+sums[r[i+k]]){ans[0]=l[i-k];ans[1]=i;ans[2]=r[i+k];}returnans;}}