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

/*input
32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<long, long>
#define vi vector<long long>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define mod 998244353
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repi(i, a, b) for (ll i = a; i >= b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define fIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define clr(x) memset(x, 0, sizeof(x))
#define debug(x) cerr << #x << "=" << x << endl
#define debug2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define TIME                                                                   \\
  cerr << "\\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
// #define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,
// tree_order_statistics_node_update>;

ll sub(ll a, ll b) { return (a - b + hell) % hell; }
ll add(ll a, ll b) { return (a + b) % hell; }
ll mul(ll a, ll b) { return (a * b) % hell; }
/*vector<ll>adj[100005];
bool vis[100005];
ll siz,edges;
void dfs(ll i)
{
    vis[i]=1;
    //siz++;
    for(ll j=0;j<adj[i].size();j++)
    {
        edges++;
        if(!vis[adj[i][j]])
            dfs(adj[i][j]);
    }
}
ll mpow(ll base, ll exp) {
  base %= hell;
  ll result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % hell;
    base = ((ll)base * base) % hell;
    exp >>= 1;
  }
  return result;
}*/
int main() {
  fIO;
  ll n, sum = 0, t = 1, m, ans = 0;

  // cin >> t;
  while (t--) {
    // string s;
    cin >> n;
    vi a(n + 1);
    rep(i, 1, n + 1) cin >> a[i];
    mi m;
    m[a[1] + 1]++;
    rep(i, 2, n + 1) {
      if (i > a[i]) {
        ans += m[i - a[i]];
      }
      m[a[i] + i]++;
    }
    cout << ans;
  }
  TIME;
}

Error: TLE

Fixed submission:

/*input
32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<long, long>
#define vi vector<long long>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define mod 998244353
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repi(i, a, b) for (ll i = a; i >= b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define fIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define clr(x) memset(x, 0, sizeof(x))
#define debug(x) cerr << #x << "=" << x << endl
#define debug2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define TIME                                                                   \\
  cerr << "\\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
// #define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,
// tree_order_statistics_node_update>;

ll sub(ll a, ll b) { return (a - b + hell) % hell; }
ll add(ll a, ll b) { return (a + b) % hell; }
ll mul(ll a, ll b) { return (a * b) % hell; }
/*vector<ll>adj[100005];
bool vis[100005];
ll siz,edges;
void dfs(ll i)
{
    vis[i]=1;
    //siz++;
    for(ll j=0;j<adj[i].size();j++)
    {
        edges++;
        if(!vis[adj[i][j]])
            dfs(adj[i][j]);
    }
}
ll mpow(ll base, ll exp) {
  base %= hell;
  ll result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % hell;
    base = ((ll)base * base) % hell;
    exp >>= 1;
  }
  return result;
}*/
int main() {
  fIO;
  ll n, sum = 0, t = 1, m, ans = 0;

  // cin >> t;
  while (t--) {
    // string s;
    cin >> n;
    vi a(n);
    rep(i, 0, n) cin >> a[i];
    ll k = n - 1;
    while (k) {
      for (ll i = 0; i + k < n; i++)
        if (a[i] + a[i + k] == k)
          ans++;
      k--;
    }
    cout << ans;
  }
  TIME;
}