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

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>; // グラフ型
typedef long long ll;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)

/*
 // 深さ優先探索
vector<bool> seen;
int dfs(const Graph &G, ll v) {
    seen[v] = true; // v を訪問済にする
    int score = 0;

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) {
        if (seen[next_v]) continue; // next_v が探索済だったらスルー

        // 負荷さ優先探索中の処理



        dfs(G, next_v); // 再帰的に探索, 毎回mat渡してええの？
    }
    return score;
} */

/*
//桁数の計算
ll keta(ll num){
  ll ans= 0 ;
  ll rem;

  for (ll i = 4; i >= 0 ; i--){
    rem = pow(10,i);
    ans += (num / rem);
    num = num % rem;
  }
  return ans;
}
*/

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  vector<ll> save(N + 1);

  for (ll i = 1; i < N + 1; i++) {
    cin >> A[i];
  }

  ll save_sum = 0;
  for (ll i = N; i > 1; i--) {
    save_sum += A[i];
    save_sum = save_sum % 1000000007;
    save[i] = save_sum;
    // cout << "save_sum:" << save_sum << endl;
  }

  ll sum = 0;

  for (ll i = 1; i < N; i++) {
    sum += (A[i] * save[i + 1]) % 1000000007;
    sum = sum % 1000000007;
    // cout << "add:"<< A[i] * save[i+1] << endl;
  }

  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>; // グラフ型
typedef long long ll;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)

/*
 // 深さ優先探索
vector<bool> seen;
int dfs(const Graph &G, ll v) {
    seen[v] = true; // v を訪問済にする
    int score = 0;

    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) {
        if (seen[next_v]) continue; // next_v が探索済だったらスルー

        // 負荷さ優先探索中の処理



        dfs(G, next_v); // 再帰的に探索, 毎回mat渡してええの？
    }
    return score;
} */

/*
//桁数の計算
ll keta(ll num){
  ll ans= 0 ;
  ll rem;

  for (ll i = 4; i >= 0 ; i--){
    rem = pow(10,i);
    ans += (num / rem);
    num = num % rem;
  }
  return ans;
}
*/

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);

  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }

  ll sum = 0;

  for (ll i = 0; i < N; i++) {
    for (ll j = i + 1; j < N; j++) {
      if (i != j) {
        sum += (A[i] * A[j]) % 1000000007;
      }
      sum = sum % 1000000007;
    }
  }

  cout << sum << endl;

  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int mn = 2e5 + 7, mod = 1e9 + 7;
long long a[mn], sum[mn], ans;
int n;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    sum[i] = (sum[i - 1] + a[i]) % mod;
  }
  for (int i = 1; i <= n; ++i)
    ans = (ans + a[i] * (sum[n] - sum[i] + mod) % mod) % mod;
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int mn = 1e5 + 7, mod = 1e9 + 7;
long long a[mn], sum[mn], ans;
int n;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    sum[i] = (sum[i - 1] + a[i]) % mod;
  }
  for (int i = 1; i <= n; ++i)
    ans = (ans + a[i] * (sum[n] - sum[i] + mod) % mod) % mod;
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<int, Pii> P1;

#define rep(i, n) for (int i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define mp1(a, b, c) P1(a, P(b, c))
#define get_arsize(a) (sizeof(a) / sizeof(a[0]))

const ll MOD = 1000000007;
const double EPS = 1e-10;
const int MAX = 200000;

int main() {
  ll n;
  cin >> n;
  vector<ll> a;
  rep(i, n) {
    ll t;
    cin >> t;
    a.push_back(t);
  }
  ll ans = 0;
  vector<ll> sum;
  for (int j = 0; j < n; ++j) {
    sum.push_back(a[j]);
    if (j > 0) {
      sum[j] = (sum[j - 1] + a[j]);
      sum[j] %= MOD;
    }
  }
  for (int i = 1; i < n; ++i) {
    ll tmp = sum[i - 1] * a[i];
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<int, Pii> P1;

#define rep(i, n) for (int i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define mp1(a, b, c) P1(a, P(b, c))
#define get_arsize(a) (sizeof(a) / sizeof(a[0]))

const ll MOD = 1000000007;
const double EPS = 1e-10;
const int MAX = 200000;

int main() {
  ll n;
  cin >> n;
  vector<ll> a;
  rep(i, n) {
    ll t;
    cin >> t;
    a.push_back(t);
  }
  ll ans = 0;
  for (int i = 1; i < n; ++i) {
    ll tmp = 0;
    for (int j = 0; j < i; ++j) {
      // cout << "i " << i << " j " << j << endl;
      tmp += a[j];
      tmp %= MOD;
    }
    tmp *= a[i];
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef vector<long long int> vint;
typedef vector<vint> vvint;
typedef long long int ll;
#define MOD 1000000007

static vint arr;

ll gcdExtended(ll a, ll b, ll *x, ll *y) {
  // Base Case
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  ll x1, y1; // To store results of recursive call
  ll gcd = gcdExtended(b % a, a, &x1, &y1);

  // Update x and y using results of recursive
  // call
  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}

ll modInverse(ll b) {
  ll x, y; // used in extended GCD algorithm
  ll g = gcdExtended(b, MOD, &x, &y);

  // Return -1 if b and m are not co-prime
  if (g != 1)
    return -1;

  // m is added to handle negative x
  return (x % MOD + MOD) % MOD;
}

void divide(ll &a, ll b) {
  a = a % MOD;
  a = (modInverse(b) * a) % MOD;
}

ll solve(void) {
  ll res = 0, n = arr.size(), curr = 0;

  for (int i = 1; i < n; i++) {
    curr = (curr + ((arr[0] * arr[i]) % MOD)) % MOD;
  }

  res = curr;

  for (int i = 1; i < n; i++) {
    divide(curr, arr[i - 1]);
    curr = (curr - arr[i]) % MOD;
    if (curr < 0)
      curr += MOD;

    curr = (curr * arr[i]) % MOD;
    res = (res + curr) % MOD;
  }

  return res;

  // unordered_map<ll, ll> bucket, product;

  // for(ll i:arr)
  // 	bucket[i]++;

  // for(auto itr = bucket.begin(); itr != bucket.end(); itr++){

  // }
}

int main() {
  int n;
  while (cin >> n) {
    arr.resize(n);
    for (int i = 0; i < n; i++)
      cin >> arr[i];

    cout << solve() << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<long long int> vint;
typedef vector<vint> vvint;
typedef long long int ll;
#define MOD 1000000007

static vint arr;

ll solve(void) {
  ll res = 0, n = arr.size();

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      res = (res + ((arr[i] * arr[j]) % MOD)) % MOD;
    }
  }

  return res;

  // unordered_map<ll, ll> bucket, product;

  // for(ll i:arr)
  // 	bucket[i]++;

  // for(auto itr = bucket.begin(); itr != bucket.end(); itr++){

  // }
}

int main() {
  int n;
  while (cin >> n) {
    arr.resize(n);
    for (int i = 0; i < n; i++)
      cin >> arr[i];

    cout << solve() << "\\n";
  }

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define endl '\\n'

const ll MOD = 1000000007;

int main() {
  int n;
  scanf("%d", &n);
  vector<ll> a(n);
  for (ll &i : a)
    scanf("%lld", &i);
  ll ans = 0;
  for (ll i : a) {
    ans += i;
    ans = ans % MOD;
  }
  ans *= ans;
  ans = ans % MOD;
  for (ll i : a) {
    ans -= i * i;
    if (ans < 0) {
      ll temp = (-ans + MOD - 1) / MOD;
      ans += temp * MOD;
    }
    ans = ans % MOD;
  }
  ans *= 500000004;
  ans = ans % MOD;
  printf("%lld", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define endl '\\n'

const ll MOD = 1000000007;

int main() {
  int n;
  scanf("%d", &n);
  vector<ll> a(n);
  for (ll &i : a)
    scanf("%lld", &i);
  ll ans = 0;
  for (ll i : a) {
    ans += i;
    ans = ans % MOD;
  }
  ans *= ans;
  ans = ans % MOD;
  for (ll i : a) {
    ans -= i * i;
    while (ans < 0)
      ans += MOD;
    ans = ans % MOD;
  }
  ans *= 500000004;
  ans = ans % MOD;
  printf("%lld", ans);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
static const int MOD = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[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 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
static const int MOD = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += A[i] * A[j];
      ans %= MOD;
    }
  }

  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ff first
#define ss second
#define pb push_back

ll const lim = 2e5 + 5;
ll const mod = 1000000007;
ll n, arr[lim + 5], pref[lim + 5];

ll solve() {
  ll sum = 0;
  for (int i = 1; i <= n; i++)
    pref[i] = (arr[i] + pref[i - 1]);

  for (int i = 1; i <= n; i++) {
    sum = (sum + ((arr[i] % mod) * ((pref[n] - pref[i]) % mod)) % mod) % mod;
  }
  return sum;
}

int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> arr[i];
  ll ans = (solve() + mod) % mod;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define ll long long

int const mod = 1e9 + 7;

void solve() {
  ll n, sum = 0;
  scanf("%lld", &n);
  ll arr[n + 5];
  for (int i = 0; i < n; i++)
    scanf("%lld", &arr[i]);

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  int t = 1, no = 0;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

long long mod = 1000000007;

int main() {
  int n;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  long long sum = 0;
  long long acc = 0;
  for (int i = 0; i < n; ++i) {
    sum += (a[i] * acc) % mod;
    acc = (acc + a[i]) % mod;
  }
  cout << sum % mod << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long mod = 1000000007;

int main() {
  int n;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      sum += (a[i] * a[j]) % mod;
    }
    sum = sum % mod;
  }
  cout << sum % mod << "\\n";
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int mod = 1e9 + 7;

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  ll sum = 1;
  ll a[n + 5];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = (ans % mod) + ((a[i] * a[j]) % mod);
    }
  }
  cout << ans % mod << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pi 3.14159265358979
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

int main() {
  ll n, sum = 0, summ = 0, a[300005];
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    summ += a[n - 1 - i];
    summ %= mod;
    sum += (a[n - 2 - i] * summ) % mod;
    sum %= mod;
  }
  cout << sum;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pi 3.14159265358979
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;

int main() {
  ll n, sum = 0, summ = 0, a[300005];
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    summ = 0;
    for (int j = i + 1; j < n; j++) {
      summ += a[j];
      summ %= mod;
    }
    sum += (a[i] * summ) % mod;
    sum %= mod;
  }
  cout << sum;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

ll A[200001], S[200001];

int main() {
  ll N;
  ll MOD = 1000000007;

  cin >> N;
  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
    A[i] %= MOD;
  }

  S[N] = A[N];
  for (ll i = N - 1; i >= 2; i--) {
    S[i] = (S[i + 1] + A[i]) % MOD;
  }
  /*
  for (ll i = 1; i <= N; i++) {
      printf("%ld: %ld, %ld\\n", i, A[i], S[i]);
  }
  */
  ll sum = 0;
  for (ll i = 1; i <= N - 1; i++) {
    ll tmp = (A[i] * S[i + 1]) % MOD;
    sum += tmp;
    // printf("%ld * %ld = %ld, %ld\\n", A[i], S[i+1], tmp, sum);
    sum %= MOD;
  }

  cout << sum << endl;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

int main() {
  ll N;
  vector<ll> A, S;
  ll MOD = 1000000007;

  cin >> N;
  A.push_back(0);
  S.push_back(0);
  S.push_back(0);
  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
    A[i] %= MOD;
  }

  S[N] = A[N];
  for (ll i = N - 1; i >= 2; i--) {
    S[i] = (S[i + 1] + A[i]) % MOD;
  }
  /*
  for (ll i = 1; i <= N; i++) {
      printf("%ld: %ld, %ld\\n", i, A[i], S[i]);
  }
  */
  ll sum = 0;
  for (ll i = 1; i <= N - 1; i++) {
    ll tmp = (A[i] * S[i + 1]) % MOD;
    sum += tmp;
    // printf("%ld * %ld = %ld, %ld\\n", A[i], S[i+1], tmp, sum);
    sum %= MOD;
  }

  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<ld> vld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

int main() {
  fastIO;
  const int mod = 1e9 + 7;
  int n;
  cin >> n;
  ll ans = 0, sum = 0;
  vll a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i > 0)
      ans += sum * a[i] % mod, ans %= mod;
    sum += a[i];
    sum %= mod;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<ld> vld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

int main() {
  fastIO;
  const int mod = 1e9 + 7;
  int n;
  cin >> n;
  ll ans = 0;
  vll a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = 0; j < i; j++)
      ans += a[i] * a[j] % mod, ans %= mod;
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;

const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main(void) {
  ll n;
  cin >> n;
  mint sum = 0;
  mint ans = 0;
  vector<mint> a(n);
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  rep(i, n) {
    sum = sum - a[i];
    ans += a[i] * sum;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;

const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main(void) {
  ll n;
  cin >> n;
  vector<mint> a(n);
  rep(i, n) { cin >> a[i]; }
  mint m = 0;
  mint x = 0;
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      x = a[i] * a[j];
      m += x;
    }
  }
  cout << m << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define vecll vector<long long>
#define vec2ll vector<vector<long long>>
#define vecchar vector<char>
#define vecstr vector<string>
#define vec2str vector<vector<string>>
#define vecbool vector<bool>
#define vec2bool vector<vector<bool>>
#define pairll pair<ll, ll>
#define vecpairll vector<pair<long long, long long>>
#define forll(s, a, b) for (long long s = a; s < b; s++)
#define forllde(s, a) for (long long s = a; s > 0; s--)

#define Pi 3.1415926535897932384626

int main() {
  ll n, ans = 0;
  string s, t;
  bool flag = false;

  cin >> n;

  vecll a(n), buf3(n);

  forll(i, 0, n) cin >> a[i];

  sort(a.begin(), a.end());

  ll buf = 0;

  forll(i, 0, a.size()) { buf += a[i]; }

  ll buf2 = 0;

  forll(i, 0, a.size()) {
    buf2 += a[i];
    buf3[i] = buf - buf2;
    buf3[i] %= 1000000007;
  }

  forll(i, 0, a.size()) {
    ans += buf3[i] * a[i];
    ans %= 1000000007;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define vecll vector<long long>
#define vec2ll vector<vector<long long>>
#define vecchar vector<char>
#define vecstr vector<string>
#define vec2str vector<vector<string>>
#define vecbool vector<bool>
#define vec2bool vector<vector<bool>>
#define pairll pair<ll, ll>
#define vecpairll vector<pair<long long, long long>>
#define forll(s, a, b) for (long long s = a; s < b; s++)
#define forllde(s, a) for (long long s = a; s > 0; s--)

#define Pi 3.1415926535897932384626

int main() {
  ll n, ans = 0;
  string s, t;
  bool flag = false;

  cin >> n;

  vecll a(n);

  forll(i, 0, n) cin >> a[i];

  forll(i, 0, a.size() - 1) {
    forll(j, i + 1, a.size()) {
      ll buf = (a[i] * a[j]) % 1000000007;
      ans += buf;
      if (ans > 1000000007)
        ans %= 1000000007;
    }
  }

  cout << ans;
}