alexjercan/bugnet huggingface dataset:
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll m = (1e9 + 7);
int main() {
  ll n;
  ll sum = 0;
  cin >> n;
  ll pref[n];
  vector<ll> a;
  for (int i = 0; i < n; i++) {
    ll v;
    cin >> v;
    a.push_back(v);
    sum += a[i];
    pref[i] = sum;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll p = a[i];
    ll q = pref[n - 1] - pref[i];
    ll z = ((p % m) * (q % m)) % m;
    ans = ((ans % m) + (z % m)) % m;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define m 1000000007
int main() {
  ll n;
  cin >> n;
  vector<ll> v;
  for (ll i = 0; i < n; i++) {
    ll p;
    cin >> p;
    v.push_back(p);
  }
  ll sum = 0;
  for (ll i = 0; i < n - 1; i++) {
    for (ll j = i + 1; j < n; j++) {
      sum += ((v[i] % m) * (v[j] % m)) % m;
    }
  }
  cout << sum % m;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long a[222222];
long long d = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  long long sum = 0, he = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i] * he;
    he += a[i];
    sum %= d;
    he %= d;
  }

  cout << sum;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a[222222];
long long d = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    long long he = 0;
    for (int j = i + 1; j < n; j++) {
      he = (he + a[j]) % d;
    }
    sum += (a[i] * he) % d;
    sum %= d;
  }
  cout << sum;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 1000000007
ll modexpo(ll a, ll b) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    b >>= 1;
    a = (a * a) % mod;
  }
  return res % mod;
}

void solve() {
  ll n;
  cin >> n;
  ll a[n];
  // ll hash[]
  map<ll, ll> mp;
  vector<ll> arr;

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    // if(mp[a[i]]==0){
    //     arr.push_back(a[i]);
    // }
    // mp[a[i]]++;
    sum = (sum + a[i]) % mod;
  }
  // n = arr.size();
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    sum = (sum - a[i] + mod) % mod;
    ll p = ((sum) % mod * (a[i]) % mod) % mod;
    ans = (ans + p) % mod;
  }
  cout << ans << '\\n';
}
int main() {
  IOS int tt;
  tt = 1;
  // cin>>tt;
  while (tt--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

void solve() {
  ll n;
  cin >> n;
  ll mod = 1e9 + 7;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ll p = ((a[i]) % mod * (a[j]) % mod) % mod;
      sum = (sum + p) % mod;
    }
  }
  cout << sum << '\\n';
}
int main() {
  IOS int tt;
  tt = 1;
  // cin>>tt;
  while (tt--) {
    solve();
  }
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;

int main() {
  int N;
  int mod = 1000000007;
  cin >> N;
  vector<int> A(N);
  vector<long long> B(N + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    B.at(i + 1) = B.at(i) + A.at(i);
  }

  long ans = 0;

  for (int i = 0; i < N; i++) {
    long long sum = (B.at(N) - B.at(i + 1)) % mod;
    ans += A.at(i) * sum;
    ans %= mod;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;

int main() {
  int64_t N;
  Bint sum = 0;
  cin >> N;
  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  for (int64_t i = 0; i < N - 1; i++) {
    Bint p = 0;
    for (int64_t j = i + 1; j < N; j++) {
      p += A.at(j);
    }
    sum += A.at(i) * p;
  }
  cout << sum % 1000000007 << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0), cin.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 5;
const int inf = ~0u >> 1;
typedef pair<int, int> P;
#define REP(i, a, n) for (int i = a; i < (n); ++i)
#define PER(i, a, n) for (int i = (n)-1; i >= a; --i)
const ll mod = 1e9 + 7;
ll a[maxn];
ll sum[maxn];
int main() {
  IO;
  int n;
  cin >> n;
  REP(i, 1, n + 1) {
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  ll ans = 0;
  REP(i, 2, n + 1) {
    ans = (((a[i] % mod) * (sum[i - 1] % mod) % mod) + ans) % mod;
  }
  cout << ans % mod << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <set>
#include <sstream>
#include <stack>
#include <stack>
#include <utility>
#include <vector>
#include <vector>
using namespace std;
typedef long long ll;
const ll inf = 1e17;
typedef unsigned long long ull;
#define scn(a) scanf("%d", &a)
#define scd(a) scanf("%lf", &a)
#define scl(a) scanf("%lld", &a)
#define ptf(a) printf("%d\\n", a)
#define mes(a, b) memset(a, b, sizeof(a))
#define fon(s, n) for (int i = s; i <= n; i++)
#define range(i, a, b) for (int i = a; i <= b; ++i)
#define rerange(i, a, b) for (int i = a; i >= b; --i)
// #define N 100010
// const ll p =998244353;
// const int S=20;
ll gcd(ll a, ll b) { return b > 0 ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll q_pow(ll a, ll b, ll mod) {
  ll ans = 1, res = a;
  while (b) {
    if (b & 1)
      ans = ans * res % mod;
    res = res * res % mod;
    b >>= 1;
  }
  return ans % mod;
}
// ll a[100005];
// int cnt[105]={0};
// char s[1005];
// char t[1005];
ll a[200005];
const ll modp = 1e9 + 7;
int main() {
  // int a, b, c, d, n, t;
  // int t;
  //  //double n,m,x,y;
  //  scn(t);
  //  while (t--)
  //  {
  //  	int n;scn(n);
  //  	for(int i=0;i<n;i++) scl(a[i]);
  //  	sort(a,a+n);
  //  	ll ans1=a[n-1]*a[n-2]*a[n-3]*a[n-4]*a[n-5];
  //  	ll ans2=a[n-1]*a[n-2]*a[n-3]*a[1]*a[0];
  //  	ll ans3=a[n-1]*a[3]*a[2]*a[1]*a[0];
  //  	ll ans=max(ans1,ans2);
  //  	ans=max(ans,ans3);
  //  	printf("%lld\\n",ans);
  //  }
  int n;
  scn(n);
  ll ans = 0;
  for (int i = 0; i < n; i++)
    scl(a[i]);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += ((a[i] * a[j]) % modp);
      ans %= modp;
    }
  }
  ans %= modp;
  printf("%lld", ans);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

ll moduloMul(ll a, ll b) {
  ll res = 0;
  a %= mod;
  while (b) {
    if (b & 1)
      res = (res + a) % mod;
    a = (2 * a) % mod;
    b >>= 1;
  }
  return res;
}

int main() {
  ll n, *a, i, ans, sum;
  cin >> n;
  a = new ll[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 1, sum = 0; i < n; i++)
    sum = sum + a[i];
  for (i = 0, ans = 0; i < n - 1; i++) {
    ans = (ans + moduloMul(a[i], sum)) % mod;
    sum = sum - a[i + 1];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  ll n, *a, i, j, ans;
  cin >> n;
  a = new ll[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0, ans = 0; i < n; i++)
    for (j = i + 1; j < n; j++)
      ans = (ans + a[i] * a[j]) % 1000000007;
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
// #define endl "\\n";
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
const long long int m = pow(10, 9) + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vector<ll> vec(n);
  for (int i = 0; i < n; i++)
    cin >> vec[i];
  ll s = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    ans += s * vec[i];
    ans %= m;
    s += vec[i];
    s %= m;
  }

  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n";
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
const long long int m = pow(10, 9) + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ull sum = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum = (sum % m + (a[i] % m * a[j] % m) % m) % m;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long long unsigned int llu;
#define pb push_back
#define PI 3.1415926535897932384626433832795
#define mod 1000000007
#define F first
#define S second
#define ALL(a) (a).begin(), (a).end()
#define YUGEN                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define lp(i, start, end) for (int i = start; i < end; i++)
#define MAX 105
#define BPC(x) __builtin_popcountll(x)
#define test()                                                                 \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define gcd(a, b) __gcd(a, b)
#define SIZE 26
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int main() {
  YUGEN;
  // test()
  // {
  // ll a,b;
  // cin>>a>>b;
  // ll ans=0;
  // if(a<b)
  //{
  //  int x=(b-a);
  //  if(x&1)
  //   ans++;
  //   else
  //   ans+=2;
  // }
  //   else if(a==b)
  //   ans=0;
  //   else if(a>b)
  //   {
  //     int x=(a-b);
  //     if(x&1)
  //     ans+=2;
  //     else
  //     ans++;

  //   }
  //   cout<<ans<<endl;
  // }
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
    sum %= mod;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    sum -= a[i];
    if (sum < 0)
      sum += mod;
    ans += a[i] * sum;
    ans %= mod;
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long long unsigned int llu;
#define pb push_back
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define F first
#define S second
#define ALL(a) (a).begin(), (a).end()
#define YUGEN                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define lp(i, start, end) for (int i = start; i < end; i++)
#define MAX 105
#define BPC(x) __builtin_popcountll(x)
#define test()                                                                 \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define gcd(a, b) __gcd(a, b)
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int main() {
  YUGEN;
  ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++)
      sum = ((sum % MOD) + ((a[i] % MOD) * (a[j] % MOD))) % MOD;
  }
  sum = sum % MOD;
  cout << sum;

  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
constexpr long long MOD = 1000000007;
long long N;
vector<long long> A;
int main() {
  long long ans = 0;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    long long a;
    cin >> a;
    A.push_back(a);
  }
  long long *A_sum = new long long[200001];
  A_sum[N] = 0;
  for (int i = N - 1; i >= 0; --i) {
    A_sum[i] = (A_sum[i + 1] + A[i]) % MOD;
  }
  for (int i = 0; i < N - 1; ++i) {
    ans = (ans + (A[i] * A_sum[i + 1]) % MOD) % MOD;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
constexpr long long mod = 1000000007;
long long N;
vector<long long> A;
int main() {
  long long ans = 0;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    long long a;
    cin >> a;
    A.push_back(a);
  }
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      ans += A[i] * A[j];
      if (ans > mod)
        ans %= mod;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int pref[n + 1];
  pref[0] = 0;
  for (int i = 1; i <= n; ++i) {
    pref[i] = (pref[i - 1] + a[i - 1]) % mod;
  }
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += (1LL * a[i] * ((pref[n] - pref[i + 1] + mod) % mod)) % mod;
  }
  ans %= mod;
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  int a[n];
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    for (int j = i - 1; j > -1; --j) {
      ans += (1LL * a[i] * a[j]) % mod;
      ans %= mod;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const long long N = 1e9 + 7;
int main() {
  // your code goes here
  int n;
  cin >> n;
  int arr[n + 1];
  long long ans = 0;
  long long sum[n + 1];
  sum[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    sum[i] = (sum[i - 1] + arr[i]) % N;
  }

  for (int i = 1; i <= n - 1; i++) {
    ans = ans + ((arr[i]) * (((sum[n] - sum[i]) + N) % N)) % N;
    ans = (ans + N) % N;
  }
  ans = (ans + N) % N;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const unsigned long N = 1e9 + 7;
int main() {
  // your code goes here
  int n;
  cin >> n;
  unsigned long long arr[n + 2];
  unsigned long long ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = ans + (arr[i] % N * arr[j] % N) % N;
      ans %= N;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int mod = 1000000007;
  int n;
  long long int a[200005];
  cin >> n;
  for (int x = 0; x < n; ++x) {
    cin >> a[x];
  }
  long long int sum = 0;
  for (int i = 1; i < n; ++i) {
    sum += a[i];
    sum = sum % mod;
  }
  long long int ans = 0;
  for (int j = 0; j < n - 1; ++j) {
    ans += a[j] * sum;
    ans %= mod;
    sum -= a[j + 1];
    if (sum < 0) {
      sum += mod;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  long long int a[200005];
  cin >> n;
  for (int x = 0; x < n; ++x) {
    cin >> a[x];
  }
  long long int sum = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      sum = sum + a[i] * a[j];
      sum = sum % 1000000007;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#define MOD (1000000000 + 7)
typedef long long ll;

int main() {
  ll n, a[200000] = {0}, sum = 0, tmp = 0;
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    tmp += a[i];
    tmp %= MOD;
  }
  for (int i = 0; i < n; i++) {
    tmp -= a[i];
    if (tmp < 0) {
      tmp += MOD;
    }
    sum += (a[i] * tmp);
    sum %= MOD;
  }
  printf("%lld\\n", sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#define MOD (1000000000 + 7)
typedef long long ll;

int main() {
  ll n, tmp, a[200000] = {0}, sum = 0;
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &tmp);
    a[i] = tmp;
    if (i) {
      sum = (sum + (a[i - 1] * a[i] % MOD)) % MOD;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 2; j < n; j++) {
      sum = (sum + (a[i] * a[j] % MOD)) % MOD;
    }
  }
  printf("%lld\\n", sum);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define bitCount(num) __builtin_popcount(num)
#define pb push_back
#define ll long long
#define bye return 0;
#define f(i, a, b) for (long long i = a; i < b; i++)
#define testCases                                                              \\
  long long t;                                                                 \\
  cin >> t;                                                                    \\
  while (t--)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define newLine << "\\n"
#define all(x) (x).begin(), (x).end()
#define vll vector<long long>

int main() {
  fast

      ll n;
  cin >> n;
  vector<ll> a(n, 0);
  f(i, 0, n) cin >> a[i];
  ll ans = 0;

  vector<ll> pSum(n, 0);
  pSum[n - 1] = a[n - 1];

  for (ll j = n - 2; j >= 0; j--) {
    pSum[j] = pSum[j + 1] + a[j];
    pSum[j] %= 1000000007;
  }

  for (ll j = 1; j < n; j++) {
    ll tempAns = a[j - 1] * pSum[j];
    ans += tempAns;
    ans %= 1000000007;
  }

  cout << ans << endl;

  bye
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define bitCount(num) __builtin_popcount(num)
#define pb push_back
#define ll long long
#define bye return 0;
#define f(i, a, b) for (long long i = a; i < b; i++)
#define testCases                                                              \\
  long long t;                                                                 \\
  cin >> t;                                                                    \\
  while (t--)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define newLine << "\\n"
#define all(x) (x).begin(), (x).end()
#define vll vector<long long>

int main() {
  fast

      ll n;
  cin >> n;
  vector<ll> a(n, 0);
  f(i, 0, n) cin >> a[i];
  ll ans = 0;

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ll temp = a[i] * a[j];
      temp %= 1000000007;
      ans += temp;
      ans %= 1000000007;
    }
  }

  // vector<ll> pSum(n,0);
  // pSum[n-1]=a[n-1];

  // for(int j=n-2;j>=0;j--)
  // {
  //     pSum[j]=pSum[j+1]+a[j];
  // }

  // for(int j=1;j<n;j++)
  // {
  //   ll tempAns=a[j-1]*pSum[j];
  //   tempAns%=1000000007;
  //   ans+=tempAns;
  // }
  cout << ans % 1000000007 << endl;

  bye
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <numeric>
#include <vector>

int main() {
  unsigned long long n;
  std::cin >> n;
  std::vector<unsigned long long> vec;

  for (unsigned int i = 0; i < n; ++i) {
    unsigned long long q;
    std::cin >> q;
    vec.push_back(q);
  }
  unsigned long long local_sum = 0;
  unsigned long long sum = 0;
  for (unsigned int i = 0; i < vec.size(); ++i) {
    sum += local_sum * vec[i];
    sum %= 1000000007;
    local_sum += vec[i];
    local_sum %= 1000000007;
  }
  std::cout << sum % 1000000007 << std::endl;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

int main() {
  unsigned long long n;
  std::cin >> n;
  std::vector<long long> vec;

  for (int i = 0; i < n; ++i) {
    unsigned long long q;
    std::cin >> q;
    vec.push_back(q);
  }

  unsigned long long sum = 0;
  for (unsigned int i = 0; i < vec.size(); ++i) {
    for (unsigned int j = i + 1; j < vec.size(); ++j) {
      sum += vec[i] * vec[j];
      if (sum >= 1000000007) {
        sum %= 1000000007;
      }
    }
  }
  std::cout << sum << std::endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ll long long
template <typename T> bool PN(T x) {
  if (x <= 1)
    return false;
  if (x == 2)
    return true;
  for (int i = 2; i < sqrt(x) + 1; i++)
    if (x % i == 0)
      return false;
  return true;
}

const ll MOD = 1e9 + 7;
long long Comb(int n, int i) {
  long long ans = 1;
  if (i > n || i < 0)
    return 0;
  if (i == 0 || i == n)
    return 1;
  else {
    for (int j = 1; j <= i; ++j) {
      ans *= (n + 1 - j);
      ans /= j;
      ans %= MOD;
    }
  }
  return ans;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) {
  if (b > a)
    swap(a, b);
  T g = gcd(a, b);
  return a / g * b;
}

void solve() {
  int n;
  cin >> n;
  ll a[n];
  ll sums[n];
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  rep(i, n) {
    if (i == 0)
      sums[i] = sum - a[i];
    else {
      sums[i] = sums[i - 1] - a[i];
    }
  }
  ll ans = 0;
  rep(i, n) {
    sums[i] %= MOD;
    a[i] %= MOD;
    ans += a[i] * sums[i];
    ans %= MOD;
    // cout << sums[i] << endl;
  }
  cout << ans << endl;
}

int main(void) {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ll long long
template <typename T> bool PN(T x) {
  if (x <= 1)
    return false;
  if (x == 2)
    return true;
  for (int i = 2; i < sqrt(x) + 1; i++)
    if (x % i == 0)
      return false;
  return true;
}

const ll MOD = 1e9 + 7;
long long Comb(int n, int i) {
  long long ans = 1;
  if (i > n || i < 0)
    return 0;
  if (i == 0 || i == n)
    return 1;
  else {
    for (int j = 1; j <= i; ++j) {
      ans *= (n + 1 - j);
      ans /= j;
      ans %= MOD;
    }
  }
  return ans;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) {
  if (b > a)
    swap(a, b);
  T g = gcd(a, b);
  return a / g * b;
}

void solve() {
  int n;
  cin >> n;
  ll a[n];
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n - 1) {
    for (int j = i + 1; j < n; ++j) {
      ans += a[i] * a[j];
      ans %= MOD;
    }
  }
  cout << ans << endl;
}

int main(void) {
  solve();
  return 0;
}