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

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

#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i)
#define forr0(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i)
#define deb(x) cout << #x << "= " << x << endl
#define pb push_back
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define tr(x) for (auto it = x.begin(); x != x.end(); it++)
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MOD 1000000007

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
  fast;
  int t = 1;
  while (t--) {
    // begin code here
    int n;
    cin >> n;
    ll arr[n];
    ll sum = 0;
    for0(i, n) {
      cin >> arr[i];
      sum += arr[i];
      sum %= MOD;
    }
    ll ans = 0;
    for0(i, n) {
      sum -= arr[i];
      if (sum < 0) {
        sum += MOD;
      }
      ans += (sum * arr[i]);
      ans %= MOD;
    }
    cout << ans;
  }
}

Error: TLE

Fixed submission:

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

#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i)
#define forr0(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i)
#define deb(x) cout << #x << "= " << x << endl
#define pb push_back
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define tr(x) for (auto it = x.begin(); x != x.end(); it++)
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MOD 1000000007

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

int main() {
  fast;
  int t = 1;
  while (t--) {
    // begin code here
    int n;
    cin >> n;
    ll arr[n];
    for0(i, n) {
      cin >> arr[i];
      if (arr[i] > MOD) {
        arr[i] = arr[i] % MOD;
      }
    }
    ll sum = 0;
    ll add = 0;
    for0(i, n - 1) {
      for (int j = i + 1; j < n; j++) {
        add = (arr[i] * arr[j]) % MOD;
        if (add > MOD) {
          add %= MOD;
        }
        sum += add;
      }
    }
    cout << (sum % MOD);
  }
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long sum = 0;
  for (int i = 1; i < n; i++) {
    sum += a[i];
    sum %= mod;
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    long long temp = a[i] * sum;
    temp %= mod;
    ans += temp;
    ans %= mod;
    sum = ((sum % mod) - (a[i + 1] % mod) + mod) % mod;
  }
  cout << ans << '\\n';
}

Error: TLE

Fixed submission:

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

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

int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A.at(i);
  }
  ll ans = 0;
  ll x = 0;
  for (int i = 0; i < n; i++) {
    ans = (ans + A.at(i) * x) % 1000000007;
    x = (x + A.at(i)) % 1000000007;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> A.at(i);
  }
  ll sum = 0;
  for (int i = 1; i < N; i++) {
    for (int j = i + 1; j <= N; j++) {
      if (A.at(i) == 0 || A.at(j) == 0) {
        sum += 0;
      } else {
        // cout<<i<<j<<endl;
        // cout<<A.at(i)<<"*"<<A.at(j)<<endl;
        sum += A.at(i) * A.at(j);
        // cout<<sum<<endl;
        sum = sum % (1000000007);
        // cout<<sum<<endl;
      }
    }
  }
  // sum%=1000000007;
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main(void) {

  int n;
  cin >> n;

  vector<long long int> a(n);
  vector<long long int> s(n + 1, 0);

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

  long long int ans = 0;

  for (int i = 0; i < n; i++) {
    long long int sum = (s[n] - s[i + 1]) % 1000000007;
    ans += a[i] * sum;
    ans %= 1000000007;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

int main(void) {
  vector<long long int> a = {};

  int n;

  cin >> n;
  a.reserve(n);

  for (int i = 0; i < n; i++) {
    long long int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }

  long long int ans = 0;

  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (i == j)
        continue;
      ans += a[i] * a[j];
      ans %= 1000000007;
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;

int main() {
  int a;
  cin >> a;
  long long int b[a];
  int long long sum = 0;
  long long int ans = 0;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
    sum += b[i];
    sum %= MOD;
  }
  for (int i = 0; i < a; i++) {
    sum -= b[i];
    if (sum < 0)
      sum += MOD;
    ans += sum * b[i];
    ans %= MOD;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;

int main() {
  int a;
  cin >> a;
  long long int b[a];
  int sum = 0;
  for (int i = 0; i < a; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < a - 1; i++) {
    for (int j = i + 1; j < a; j++) {
      sum += b[i] * b[j] % MOD;
      sum %= MOD;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
ll a[2000005];
int main(void) {
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    sum -= a[i];

    ans += ((sum % inf) * (a[i] % inf));
    ans %= inf;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
ll a[2000005];
int main(void) {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += (a[i] * a[j]) % inf;
      ans %= inf;
    }
  }
  cout << ans % inf << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

static const int MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  long long sum, A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum = (sum + A[i]) % MOD;
  }

  // sum = A1 + A2 + ... + An
  // A1(A2+A3+...+An) + A2(A3+A4+...+An) + ... + An-2(An-1+An) + An-1An
  long long ans = 0;
  for (int i = 0; i < N - 1; i++) {
    sum -= A[i];
    if (sum < 0)
      sum += MOD;

    ans += A[i] * sum;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

static const 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 tmp, ans = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      tmp = A[i] * A[j] % MOD;
      ans = (ans + tmp) % MOD;
      ans = (ans + MOD) % MOD;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<ll> sum(n, 0);
  sum[0] = 0;
  for (ll i = 0; i < n - 1; ++i) {
    sum[i + 1] = (sum[i] + a[n - 1 - i]) % mod;
  }

  ll res = 0;
  for (ll i = 1; i < n; ++i) {
    res += (sum[i] * a[n - 1 - i]) % mod;
  }
  res %= mod;
  cout << res << endl;
  return 0;
}

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>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<ll> sum(n, 0);
  sum[0] = 0;
  for (ll i = 0; i < n; ++i) {
    sum[i + 1] = (sum[i] + a[n - 1 - i]) % mod;
  }

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

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

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n - 1);
  ll cpt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cpt += a[i];
  }

  for (int i = 0; i < n - 1; i++) {
    cpt -= a[i];
    b[i] = cpt % MOD;
  }

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

  ans %= MOD;

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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 - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += a[i] * a[j] % MOD;
    }
  }

  ans %= MOD;

  cout << ans << endl;

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

#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
int main(int argc, char *argv[]) {
  const LL MOD = 1000000000 + 7;
  int N, i;
  cin >> N;
  vector<LL> A(N);
  LL hj = 0;
  for (i = 0; i < N; i++) {
    cin >> A[i];
    hj += A[i];
    hj %= MOD;
  }
  LL Sum = 0;
  for (i = 0; i < N - 1; i++) {
    hj -= A[i];
    if (hj < 0) {
      hj += MOD;
    }
    LL B = A[i] * hj;
    B %= MOD;
    Sum += B;
    Sum %= MOD;
  }
  cout << Sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
int main(int argc, char *argv[]) {
  const LL MOD = 1000000007;
  int N, i, j;
  cin >> N;
  vector<LL> A(N);
  for (i = 0; i < N; i++) {
    cin >> A[i];
  }
  LL Sum = 0;
  for (i = 0; i < N - 1; i++) {
    for (j = i + 1; j < N; j++) {
      LL AA = A[i] * A[j];
      Sum += AA;
      Sum %= MOD;
    }
  }
  cout << Sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int MOD = 1000000007, N = 200010;
long long n, a[N], b[N];
int main() {
  //	freopen("1.txt","r",stdin);
  long long s = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
    s %= MOD;
    b[i + 1] = s;
  }
  b[0] = 0;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    sum += b[i] * a[i] % MOD;
    sum %= MOD;
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int MOD = 1000000007, N = 200010;
long long n, a[N];
int main() {
  //	freopen("1.txt","r",stdin);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long sum = 0;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < i; j++) {
      sum += a[i] * a[j] % MOD;
      sum %= MOD;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <list>
#include <string>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
typedef long long ll;
bool aaa[11111111];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  ll n, c, m, w = 0, memo = 0, memo2 = 0, sx = 0, sy = 1, ans = 0, gx, gy;
  string ka, ss;
  cin >> n;
  vector<ll> A(n), B(n);
  rep(i, n) cin >> A[i];
  B[n - 1] = A[n - 1];
  rep(i, n - 1) { B[n - i - 2] = (B[n - i - 1] + A[n - i - 2]) % 1000000007; }
  while (w < n - 1) {
    ans = (ans + (A[w] * B[w + 1])) % 1000000007;
    w++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <list>
#include <string>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
typedef long long ll;
bool aaa[11111111];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  ll n, c, m, w = 0, memo = 0, memo2 = 0, sx = 0, sy = 1, ans = 0, gx, gy;
  string ka, ss;
  cin >> n;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  while (w < n - 1) {
    c = w + 1;
    while (1) {
      if (c == n)
        break;
      ans = (ans + (A[c] * A[w] % 1000000007)) % 1000000007;
      c++;
    }
    w++;
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll N, mod;

  mod = 1000000007;
  cin >> N;
  vector<ll> A(N);
  for (auto &x : A)
    cin >> x;

  int i, j;
  ll res = 0;
  ll tmp = 0;
  for (i = N - 2; i >= 0; i--) {
    tmp += A[i + 1];
    tmp = tmp % mod;
    res += A[i] * tmp;
    res = res % mod;
  }

  cout << res << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  ll N, mod;

  mod = 1000000007;
  cin >> N;
  vector<ll> A(N);
  vector<vector<ll>> AA(N, vector<ll>(N));
  vector<vector<bool>> f(N, vector<bool>(N, false));
  for (auto &x : A)
    cin >> x;

  int i, j;
  ll res = 0;
  ll tmp;
  for (i = N - 2; i >= 0; i--) {
    tmp += A[i + 1];
    tmp = tmp % mod;
    res += A[i] * tmp;
    res = res % mod;
  }

  cout << res << endl;

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

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<long long> B(N + 1, 0); // 累積和テーブル
  int mod = 1000000007;

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

  long ans = 0;

  // i について全探索する
  for (int i = 0; i < N; i++) {
    // 累積和を使ってA[i+1] + ... + A[N] を求める
    long long sum = (B[N] - B[i + 1]) % mod;

    ans += A[i] * sum;
    ans %= mod;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

const int mod = 1000000007;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
#define ll long long

int main() {
  int a;
  cin >> a;

  vector<ll> vec(a);
  for (int i = 0; i < a; i++) {
    cin >> vec[i];
  }

  ll sum = 0;
  for (int i = 0; i < a - 1; i++) {
    ll num = vec[i];

    for (int j = i + 1; j < a; j++) {
      sum += (num * vec[j]) % mod;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define lc "\\n"
#define fast_io                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define c(a, n)                                                                \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define ffor(n) for (int i = 0; i < n; i++)
#define float long double
typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef map<string, int> msi;
typedef map<int, int> mii;
typedef unordered_map<string, int> umsi;

int32_t main() {
  fast_io;
  int n;
  cin >> n;
  vi a(n);
  c(a, n);
  vi pre(n);
  int m = 1e9 + 7;
  pre[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; i--)
    pre[i] = ((a[i] % m) + (pre[i + 1] % m)) % m;
  int ans = 0;
  for (int i = 1; i < n; i++)
    ans = (ans % m + ((a[i - 1] % m) * (pre[i] % m)) % m) % m;
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lc "\\n"
#define fast_io                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define c(a, n)                                                                \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define ffor(n) for (int i = 0; i < n; i++)
#define float long double
typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef map<string, int> msi;
typedef map<int, int> mii;
typedef unordered_map<string, int> umsi;

int32_t main() {
  fast_io;
  int n;
  cin >> n;
  vi a(n);
  c(a, n);
  int ans = 0;
  int m = 1e9 + 7;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = (ans % m + ((a[i] % m) * (a[j] % m) % m) % m) % m;
    }
  }
  cout << ans;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long int
using namespace std;
signed main() {
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    int res = 0;
    int mod = 1000000007;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int pp[n];
    pp[0] = a[0];
    for (int i = 1; i < n; i++) {
      pp[i] = (pp[i - 1] + a[i]) % mod;
    }
    for (int i = 0; i < n; i++) {
      res = res % mod + (a[i] % mod * (pp[n - 1] - pp[i] + mod) % mod) % mod;
      res = res % mod;
    }
    cout << res;
  }
}

Error: TLE

Fixed submission:

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

#include <iostream>
using namespace std;

int main() {
  unsigned long long int a[200000], sum = 0, n, mod = 1000000007, s = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  for (int i = 0; i < n; i++) {
    s -= a[i];
    sum = (sum + ((a[i] % mod) * (s % mod))) % mod;
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

#include "bits/stdc++.h"
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("avx,avx2")

using namespace std;

using ll = long long;
#define all(x) begin(x), end(x)

int main() {
  ios_base::sync_with_stdio(0);
  int N;
  cin >> N;
  vector<int> A(N);
  vector<long long> B(N + 1, 0);
  int mod = 1000000007;

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

  long ans = 0;

  for (int i = 0; i < N; i++) {
    long long sum = (B[N] - B[i + 1]) % mod;

    ans += A[i] * sum;
    ans %= mod;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("avx,avx2")

using namespace std;

using ll = long long;
#define all(x) begin(x), end(x)

const int N = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<ll> A(n);
  for (int i{}; i < n; i++)
    cin >> A[i];
  ll product = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      product = (product % N + (A[i] * A[j]) % N);
  cout << product;
}