# Help me in solving SEGTHREE problem

### My issue

Is there any proof that this way of fixing 2 values will give optimal value,
may we can think of the contribution of each index gives to form the mod,by seeing those we have to greedily change is my argument, can anyone help.
If we see that way may be greedy might fail, we might have to go to dp, which one solution was given, but still what the answer the author gave is not convincing me that it would be optimal.
Below is the code of my assumption

### My code

``````#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "/debug.h"
#endif
typedef long long ll;
#define nl '\n'
#define all(v) (v).begin(), (v).end()
const int MOD = 1000000007;
#define int long long

void solve(){
int n;
cin >> n;
vector<int>v(n);
for(auto&i:v)cin >> i;
vector<int>check;
for(int i = 0;i < n-2;i++){
check.push_back(3-(v[i]+v[i+1]+v[i+2])%3);
}
int ans = 0;
//cout << check;
int size = check.size();
while(check.size()!=0 and check.front()==3)check.erase(check.begin());
while(check.size()!=0 and check.back()==3)check.pop_back();
size = check.size();
for(int i = 0;i < size;i++){
int minn = check[i];
if (i+1<size)minn = min(minn,check[i+1]);
if (i+2<size)minn = min(minn,check[i+2]);
check[i]-=minn;
if (i+1<size)check[i+1]-=minn;
int c=0;
ans+=minn+check[i];
if (i+2<size)check[i+2]-=minn;
if (check[i+1]==0){
c++;
if (check[i+2]==0)
c++;
}
i+=c;
}
//cout << check;
cout << ans << nl;

}

signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

int tt=1;
cin >> tt;
while(tt--){
solve();
}
return 0;
}

``````

Problem Link: SEGTHREE Problem - CodeChef