Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
// #include <iomanip>
// #include <string>
#include <algorithm>
#include <vector>
// #include <utility>
// #include <set>
// #include <map>
// #include <queue>
// #include <deque>
// #include <bitset>
// #include <math.h>
using namespace std;
using ll = long long;
// using ld = long double ;
using vll = vector<ll>;
using vvll = vector<vll>;
// using vc = vector<char> ;
// using vvc = vector<vc> ;
// using vb = vector<bool> ;
// using vvb = vector<vb> ;
// using pll = pair<ll,ll> ;
// #define all(v) v.begin(),v.end()
// ll mod = 1000000007 ;
// long double pie = acos(-1) ;
// ll INF = 1000000000000 ;

// void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
// void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
// ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
// ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
// ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
// void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}
// void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}
// void fix_cout(){cout << fixed << setprecision(20) ;}

int main() {
  ll n;
  cin >> n;
  vll a(n);
  for (auto &i : a)
    cin >> i;
  vvll dp(n + 1, vll(2, 0));
  dp.at(1).at(0) = dp.at(2).at(0) = 0;
  dp.at(1).at(1) = a.at(0);
  dp.at(2).at(1) = max(a.at(0), a.at(1));
  for (int i = 2; i < n; i++) {
    if (i & 1) {
      dp.at(i + 1).at(0) = max(dp.at(i).at(0), dp.at(i - 1).at(0) + a.at(i));
      dp.at(i + 1).at(1) = max(dp.at(i).at(1), dp.at(i - 1).at(1) + a.at(i));
    } else {
      dp.at(i + 1).at(0) = max(dp.at(i).at(1), dp.at(i - 1).at(0) + a.at(i));
      dp.at(i + 1).at(1) = dp.at(i - 1).at(1) + a.at(i);
    }
  }
  cout << dp.at(n).at((n + 1) & 1) << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
// #include <iomanip>
// #include <string>
#include <algorithm>
#include <vector>
// #include <utility>
// #include <set>
// #include <map>
// #include <queue>
// #include <deque>
// #include <bitset>
// #include <math.h>
using namespace std;
using ll = long long;
// using ld = long double ;
using vll = vector<ll>;
// using vvll = vector<vll> ;
// using vc = vector<char> ;
// using vvc = vector<vc> ;
// using vb = vector<bool> ;
// using vvb = vector<vb> ;
// using pll = pair<ll,ll> ;
// #define all(v) v.begin(),v.end()
// ll mod = 1000000007 ;
// long double pie = acos(-1) ;
// ll INF = 1000000000000 ;

// void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
// void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
// ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
// ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
// ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
// void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}
// void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}
// void fix_cout(){cout << fixed << setprecision(20) ;}

ll solve(vll &a, ll n, ll l, ll r) {
  vll dp(n + 2, 0);
  for (int i = 2; i <= n + 1; i++) {
    if (i % 2 == 0)
      dp.at(i) = dp.at(i - 2) + a.at(l + i - 2);
    else
      dp.at(i) = max(dp.at(i - 1), dp.at(i - 2) + a.at(l + i - 2));
  }
  return dp.at(dp.size() - 1);
}

ll ALL(vll &a, ll n, ll l, ll r) {
  ll sum = 0;
  for (int i = l; i <= r; i += 2)
    sum += a.at(i);
  return sum;
}

ll DFS(vll &a, ll n, ll l, ll r, ll k) {
  if (k == 0)
    return 0;
  if (k == 1) {
    ll res = -10000000000;
    for (int i = l; i <= r; i++)
      res = max(res, a.at(i));
    return res;
  }
  ll sub1 = solve(a, n - 1, l, r - 1);
  ll sub2 = solve(a, n - 1, l + 1, r);
  ll sub3 = ALL(a, n - 2, l + 1, r - 1);
  return max(max(sub1, sub2),
             max(sub3, DFS(a, n - 2, l + 2, r - 2, k - 2) + a.at(l) + a.at(r)));
}

int main() {
  ll n;
  cin >> n;
  vll a(n);
  for (auto &i : a)
    cin >> i;
  if (n % 2 == 0) {
    cout << solve(a, n, 0, n - 1) << endl;
  } else {
    cout << DFS(a, n, 0, n - 1, n / 2) << endl;
  }
}