Language: C++

#include<bits/stdc++.h>
#include<time.h>
#include<stdlib.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#define pb push_back
#define mp make_pair
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll,ll>
#define all(a) (a).begin(),(a).end()
#define ff first
#define ss second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define repf(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define gcd(a,b) __gcd((a),(b))
#define EPS 1e-12
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
#define sqrt sqrtl
#define PI 3.14159265
#define cbrt cbrtl
using namespace std;

ll n,dp[100005][5],prev[100005][5];
int main()
{
  ll k;
  cin>>n;
  ll a[n+5],b[n+5],c[n+5];
  ll i,j;
  for(i=1;i<=n;i++)
    cin>>a[i]>>b[i]>>c[i];
  dp[1][1]=a[1];
  dp[1][2]=b[1];
  dp[1][3]=c[1];
  prev[1][1]=1;
  prev[1][2]=2;
  prev[1][3]=3;
  for(i=2;i<=n;i++)
  {
       ll val;
       if(prev[i-1][1]==1)
       {
         val=max(dp[i-1][3],dp[i-1][2]);
          if(val==dp[i-1][3])
          {
            prev[i][1]=3;
            dp[i][1]=val+a[i];
          }
          else 
            {
              prev[i][1]=2;
              dp[i][1]=val+a[i];
            }
        }
      else if(prev[i-1][1]==2)
        {
          val=max(dp[i-1][3],dp[i-1][1]);
          if(val==dp[i-1][3])
          {
            prev[i][1]=3;
            dp[i][1]=val+a[i];
          }
          else 
            {
              prev[i][1]=1;
              dp[i][1]=val+a[i];
            }
        }
      else
      {
        val=max(dp[i-1][1],dp[i-1][2]);
        if(val==dp[i-1][1])
          {
            prev[i][1]=1;
            dp[i][1]=val+a[i];
          }
          else 
            {
              prev[i][1]=2;
              dp[i][1]=val+a[i];
            }
      }
      if(prev[i-1][2]==1)
       {
         val=max(dp[i-1][3],dp[i-1][2]);
          if(val==dp[i-1][3])
          {
            prev[i][2]=3;
            dp[i][2]=val+b[i];
          }
          else 
            {
              prev[i][2]=2;
              dp[i][2]=val+b[i];
            }
        }
      else if(prev[i-1][2]==2)
        {
          val=max(dp[i-1][3],dp[i-1][1]);
          if(val==dp[i-1][3])
          {
            prev[i][2]=3;
            dp[i][2]=val+b[i];
          }
          else 
            {
              prev[i][2]=1;
              dp[i][2]=val+b[i];
            }
        }
      else
      {
        val=max(dp[i-1][1],dp[i-1][2]);
        if(val==dp[i-1][1])
          {
            prev[i][2]=1;
            dp[i][2]=val+b[i];
          }
          else 
            {
              prev[i][2]=2;
              dp[i][2]=val+b[i];
            }
      }
      if(prev[i-1][3]==1)
       {
         val=max(dp[i-1][3],dp[i-1][2]);
          if(val==dp[i-1][3])
          {
            prev[i][3]=3;
            dp[i][3]=val+c[i];
          }
          else 
            {
              prev[i][3]=2;
              dp[i][3]=val+c[i];
            }
        }
      else if(prev[i-1][3]==2)
        {
          val=max(dp[i-1][3],dp[i-1][1]);
          if(val==dp[i-1][3])
          {
            prev[i][3]=3;
            dp[i][3]=val+c[i];
          }
          else 
            {
              prev[i][3]=1;
              dp[i][3]=val+c[i];
            }
        }
      else
      {
        val=max(dp[i-1][1],dp[i-1][2]);
        if(val==dp[i-1][1])
          {
            prev[i][3]=1;
            dp[i][3]=val+c[i];
          }
          else 
            {
              prev[i][3]=2;
              dp[i][3]=val+c[i];
            }
      }
  }
 /* for(i=1;i<=n;i++)
  {
     for(j=1;j<=3;j++)
      cout<<dp[i][j]<<" "<<prev[i][j]<<"\n";
    cout<<"\n";
  }*/
  cout<<max(dp[n][1],max(dp[n][2],dp[n][3]));
  return 0; 
}


Compiler output:
a.cc: In function 'int main()':
a.cc:43:3: error: reference to 'prev' is ambiguous
   43 |   prev[1][1]=1;
      |   ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:44:3: error: reference to 'prev' is ambiguous
   44 |   prev[1][2]=2;
      |   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:45:3: error: reference to 'prev' is ambiguous
   45 |   prev[1][3]=3;
      |   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:49:11: error: reference to 'prev' is ambiguous
   49 |        if(prev[i-1][1]==1)
      |           ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:54:13: error: reference to 'prev' is ambiguous
   54 |             prev[i][1]=3;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:59:15: error: reference to 'prev' is ambiguous
   59 |               prev[i][1]=2;
      |               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:63:15: error: reference to 'prev' is ambiguous
   63 |       else if(prev[i-1][1]==2)
      |               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:68:13: error: reference to 'prev' is ambiguous
   68 |             prev[i][1]=3;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:73:15: error: reference to 'prev' is ambiguous
   73 |               prev[i][1]=1;
      |               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:82:13: error: reference to 'prev' is ambiguous
   82 |             prev[i][1]=1;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:87:15: error: reference to 'prev' is ambiguous
   87 |               prev[i][1]=2;
      |               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:91:10: error: reference to 'prev' is ambiguous
   91 |       if(prev[i-1][2]==1)
      |          ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:96:13: error: reference to 'prev' is ambiguous
   96 |             prev[i][2]=3;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:101:15: error: reference to 'prev' is ambiguous
  101 |               prev[i][2]=2;
      |               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:105:15: error: reference to 'prev' is ambiguous
  105 |       else if(prev[i-1][2]==2)
      |               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:110:13: error: reference to 'prev' is ambiguous
  110 |             prev[i][2]=3;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:115:15: error: reference to 'prev' is ambiguous
  115 |               prev[i][2]=1;
      |               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note:                 'long long int prev [100005][5]'
   31 | ll n,dp[100005][5],prev[100005][5];
      |                    ^~~~
a.cc:124:13: error: reference to 'prev' is ambiguous
  124 |             prev[i][2]=1;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:20: note: