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

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

class Graph {
  int V;
  vector<list<int>> edges;

public:
  Graph(int nodes) {
    V = nodes;
    edges.resize(nodes + 1);
  }
  void addEdge(int from, int to) { edges[from].push_back(to); }
  int min_dist(int &fin, int i, map<int, bool> &special, vector<bool> &check) {
    check[i] = true;
    multiset<int> ms;
    int k;
    for (auto it = edges[i].begin(); it != edges[i].end(); it++) {
      if (check[*it] == true)
        continue;
      k = min_dist(fin, *it, special, check);
      if (k != 0)
        ms.insert(k);
    }
    for (auto it = ms.begin(); it != ms.end(); it++) {
      fin += (*it);
      // cout << *it << " ";
    }
    // cout << endl;
    if (ms.size() % 2 == 0) {
      if (special[i])
        return 1;
      return 0;
    } else {
      if (special[i])
        return 0;
      auto it = ms.begin();
      fin -= (*it);
      return (*it) + 1;
    }
  }
};

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> arr(n);
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    ll sum = 0;
    ll temp = arr[n - 1];
    for (int i = n - 2; i >= 0; i--) {
      sum = (sum + (arr[i] % INF) * (temp % INF)) % INF;
      temp = (temp + arr[i]) % INF;
    }

    cout << sum % INF << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

class Graph {
  int V;
  vector<list<int>> edges;

public:
  Graph(int nodes) {
    V = nodes;
    edges.resize(nodes + 1);
  }
  void addEdge(int from, int to) { edges[from].push_back(to); }
  int min_dist(int &fin, int i, map<int, bool> &special, vector<bool> &check) {
    check[i] = true;
    multiset<int> ms;
    int k;
    for (auto it = edges[i].begin(); it != edges[i].end(); it++) {
      if (check[*it] == true)
        continue;
      k = min_dist(fin, *it, special, check);
      if (k != 0)
        ms.insert(k);
    }
    for (auto it = ms.begin(); it != ms.end(); it++) {
      fin += (*it);
      // cout << *it << " ";
    }
    // cout << endl;
    if (ms.size() % 2 == 0) {
      if (special[i])
        return 1;
      return 0;
    } else {
      if (special[i])
        return 0;
      auto it = ms.begin();
      fin -= (*it);
      return (*it) + 1;
    }
  }
};

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> arr(n);
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      ll temp = 0;
      for (int j = i + 1; j < n; j++) {
        temp = (temp + arr[j]) % INF;
      }
      sum = (sum + ((temp % INF) * (arr[i] % INF))) % INF;
    }

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

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

int main() {

  int n;
  cin >> n;

  long long int a = 0;
  long long int sum = 0;
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    a = a % mod;
    long long b = ((sum % mod) * (a % mod)) % mod;
    ans = (ans + (b)) % mod;
    // ans=ans%mod;
    sum += a;
  }
  cout << (ans) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
void read(vector<int> &a) {
  for (int i = 0; i < a.size(); i++)
    cin >> a[i];
}
void print(vector<int> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " ";
  cout << endl;
}
long long moduloMultiplication(long long a, long long b, long long mod) {
  long long res = 0;
  a %= mod;

  while (b) {

    if (b & 1)
      res = (res + a) % mod;
    a = (2 * a) % mod;

    b >>= 1;
  }

  return res;
}
int main() {

  int n;
  cin >> n;
  vector<int> v(n);
  read(v);
  long long int sum = 0;
  long long int m = 1000000007;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += moduloMultiplication(v[i], v[j], m);
    }
  }
  cout << (sum % m) << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
long long n, a[200010];
const int mod = 1000000007;
int main() {
  int n;
  cin >> n;
  long long s = 0, sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i <= n; i++) {
    sum -= a[i];
    s = (((a[i] % mod) * (sum % mod) % mod) + s) % mod;
  }
  cout << s << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

Error: TLE

Fixed submission:

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

#include <bits/stdc++.h>
#define LL long long int
#define PB push_back
using namespace std;
LL M = 1000000000 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  LL a[n];
  LL dp[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0)
      dp[i] = a[i];
    else {
      dp[i] = dp[i - 1] + a[i];
      dp[i] %= M;
    }
  }
  LL ans = 0;
  for (int i = 1; i < n; i++) {
    ans += dp[i - 1] * a[i];
    ans %= M;
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long int
#define PB push_back
using namespace std;
LL M = 1000000007;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  LL n;
  cin >> n;
  LL sum = 0;
  LL a[n];
  for (LL i = 0; i < n; i++)
    cin >> a[i];
  for (LL i = 0; i < n; i++) {
    for (LL j = i + 1; j < n; j++) {

      sum += a[i] * a[j];
      sum %= M;
    }
  }
  cout << sum;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) rep2(i, 0, n)
#define FOR(i) for (cin >> i; i > 0; i--)
#define elif else if
#define pb push_back
#define ll long long
#define mp make_pair
#define all(x) x.begin(), x.end()
#define re(x)                                                                  \\
  {                                                                            \\
    cout << x << endl;                                                         \\
    return 0;                                                                  \\
  }
// #define x first
// #define y second

using namespace std;
ll n, a[222222], e[222222];
ll ans = 0;
int main() {
  cin >> n >> a[0];
  e[0] = 0;
  rep2(i, 1, n) {
    cin >> a[i];
    e[i] = (a[i - 1] + e[i - 1]) % 1000000007;
  }
  rep(i, n) {
    ans += (a[i] * e[i]) % 1000000007;
    ans %= 1000000007;
  }
  re(ans) return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) rep2(i, 0, n)
#define FOR(i) for (cin >> i; i > 0; i--)
#define elif else if
#define pb push_back
#define ll long long
#define mp make_pair
#define all(x) x.begin(), x.end()
#define re(x)                                                                  \\
  {                                                                            \\
    cout << x << endl;                                                         \\
    return 0;                                                                  \\
  }
// #define x first
// #define y second

using namespace std;
ll n, a[222222];
ll ans = 0;
int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) rep2(j, i + 1, n) {
    ans += (a[i] * a[j]) % 1000000007;
    ans %= 1000000007;
  }
  re(ans) return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  int64_t N, total = 0, c = 0, naname = 0;
  int64_t MOD = 1000000007;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < N; i++) {
    c += A.at(i);
  }
  c %= MOD;
  for (int i = 0; i < N; i++) {
    total += (c * A.at(i)) % MOD;
    naname += (A.at(i) * A.at(i)) % MOD;
    total %= MOD;
    naname %= MOD;
  }
  total %= MOD;
  naname %= MOD;
  total = (total - naname) % MOD;
  if (total < 0)
    total += MOD;
  cout << total * modinv(2, MOD) % MOD << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  int64_t MOD = 1000000007;
  int64_t ans = 0, c;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      c = (A.at(i) % MOD) * (A.at(j) % MOD) % MOD;
      ans += c;
      ans %= MOD;
    }
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
int main() {
  int N;
  std::cin >> N;
  long int *A = new long int[N];
  long sum = 0;
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
    sum += A[i];
    sum %= 1000000007;
  }
  long ans = 0;
  for (int i = 0; i < N; i++) {
    sum -= A[i];
    if (sum < 0) {
      sum += 1000000007;
    }
    ans += sum * A[i];
    ans %= 1000000007;
  }
  std::cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
int main() {
  int N;
  std::cin >> N;
  long int *A = new long int[N];
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  long ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += A[i] * A[j] % 1000000007;
      ans %= 1000000007;
    }
  }
  std::cout << ans;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded


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

// syntax sugar: `for (int i = 0; i < N; ++i)`
#define rep(i, N) for (int i = 0; i < (int)(N); ++i)
#define rep2(i, startValue, N)                                                 \\
  for (int i = (int)(startValue); i < (int)(N); ++i)
// syntax sugar: `for (int i = 0; i < N; ++i)`
#define REP(type, name, beginValue, endCondValue)                              \\
  for (type name = beginValue; name < endCondValue; ++name)
// syntax sugar: 多次元vector
#define VECTOR_DIM3(T, name, d1, d2, d3, initValue)                            \\
  std::vector<std::vector<std::vector<T>>> name(                               \\
      d1, std::vector<std::vector<T>>(d2, std::vector<int>(d3, initValue)));
#define VECTOR_DIM2(T, name, d1, d2, initValue)                                \\
  std::vector<std::vector<T>> name(d1, std::vector<T>(d2, initValue));
#define VECTOR_DIM1(T, name, d1, initValue) std::vector<T> name(d1, initValue);
#define ll long long
#define ld long double
#define DUMP(v) "," #v ":" << v

const ll MOD = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  ll total = 0;
  rep(i, n) {
    cin >> a[i];
    total += a[i];
  }

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

  ll ans2 = (ll)ans;
  cout << ans2 << endl;
  return 0;
}


Error: TLE

Fixed submission:


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

// syntax sugar: `for (int i = 0; i < N; ++i)`
#define rep(i, N) for (int i = 0; i < (int)(N); ++i)
#define rep2(i, startValue, N)                                                 \\
  for (int i = (int)(startValue); i < (int)(N); ++i)
// syntax sugar: `for (int i = 0; i < N; ++i)`
#define REP(type, name, beginValue, endCondValue)                              \\
  for (type name = beginValue; name < endCondValue; ++name)
// syntax sugar: 多次元vector
#define VECTOR_DIM3(T, name, d1, d2, d3, initValue)                            \\
  std::vector<std::vector<std::vector<T>>> name(                               \\
      d1, std::vector<std::vector<T>>(d2, std::vector<int>(d3, initValue)));
#define VECTOR_DIM2(T, name, d1, d2, initValue)                                \\
  std::vector<std::vector<T>> name(d1, std::vector<T>(d2, initValue));
#define VECTOR_DIM1(T, name, d1, initValue) std::vector<T> name(d1, initValue);
#define ll long long
#define ld long double
#define DUMP(v) "," #v ":" << v

const ll MOD = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  rep(i, n) cin >> a[i];

  ll ans = 0;

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

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

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

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

  long long ans = 0;
  long long sum_comp = 0;
  for (int i = 0; i < N - 1; i++) {
    sum_comp = sumA.at(N) > sumA.at(i + 1) ? sumA.at(N) - sumA.at(i + 1)
                                           : sumA.at(N) + mod_ - sumA.at(i + 1);
    ans += A.at(i) * sum_comp;
    if (ans >= mod_)
      ans %= mod_;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  long long ans = 0;
  long long mod_ = pow(10, 9) + 7;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += A.at(i) * A.at(j);
      if (ans >= pow(10, 9) + 7)
        ans %= mod_;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

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

const int mod = 1e9 + 7;

int main(void) {
  ll 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: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

const int mod = 1e9 + 7;

int main(void) {
  ll n;
  vector<ll> A(n);

  cin >> 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;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
const double PI = acos(-1);
#define rep(i, n)                                                              \\
  for (int i = 0; i < (int)(n); i++)                                           \\
    ;
using namespace std;
const long long mod = 1000000007;
int findSumOfDigits(int n) {
  int sum = 0;
  while (n > 0) { // n が 0 になるまで
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
int main() {
  long long n;
  cin >> n;
  long long sum = 0;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a.at(i);
    sum += a.at(i);
    sum %= mod;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    sum -= a.at(i);
    if (sum < 0)
      sum += mod;
    long long num = (a.at(i) * sum) % mod;
    num %= mod;
    ans += num;
    ans %= mod;
  }
  ans %= mod;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int findSumOfDigits(int n) {
  int sum = 0;
  while (n > 0) { // n が 0 になるまで
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  long long sum = 0;
  rep(i, n) cin >> a.at(i);
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      sum += (a.at(i) * a.at(j)) % 1000000007;
      sum %= 1000000007;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define MOD 1000000007;

int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  if (N == 1) {
    cout << A[0] << endl;
    return 0;
  }
  // cout << A[1] << endl;

  unsigned long ans = 0;
  unsigned long sum_l = 0;
  for (int i = 0; i < N - 1; i++) {
    sum_l = (sum_l + A[i]) % MOD;
    ans += (sum_l * A[i + 1]) % MOD;
  }
  ans = ans % MOD;
  cout << ans << endl;
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define MOD 1000000007;

int main(void) {
  int N;
  vector<int> A(N);
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  // cout << A[1] << endl;

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

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

int main() {
  long long int n;
  cin >> n;

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

  long long int sum=0;
  for(long long int i=0;i<=n-2;i++){
      for(long long int j=i+1;j<=n-1;j++){
          sum += ((a[i]*a[j])%1000000007);
          sum %= 1000000007;
      }
  }
  cout << sum << endl;
  */

  /*----------------------------------------------
   解説：累積和を用いて処理しよう！
   a[i]*a[i+1] + a[i]*a[i+2] + ... + a[i]*a[n]
   = a[i] * (a[i+1] + a[i+2] + ... + a[n])
   この()内で累積和を用いる
  ----------------------------------------------*/
  long long int sum[n]; // 累積和 sum[0] = a[0], sum[1] = sum[0] + a[1], sum[2]
                        // = sum[1] + a[2], a[i]+...+a[j]=s[j]-s[i-1]
  sum[0] = a[0];
  for (int i = 1; i < n; i++) {
    sum[i] = sum[i - 1] + a[i];
    sum[i] %= 1000000007;
  }

  long long int ans = 0;
  long long int mod_sum;
  for (int i = 0; i < n; i++) {
    // cout << i << " " << sum[n-1] - sum[i] << endl;
    mod_sum = (sum[n - 1] - sum[i]) % 1000000007;
    if (mod_sum < 0) {
      mod_sum += 1000000007;
    }
    ans += (a[i] * mod_sum % 1000000007);
    ans %= 1000000007;
  }
  ans %= 1000000007;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long int n;
  cin >> n;

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

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

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1000000007;

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

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1000000007;

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

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

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define PI 3.14159265358979323846264338327950

int main() {
  ll N, mod = 1000000007, x = 0, num = 0, ans = 0;

  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];

  rep(i, N) { num += a[i]; }

  rep(i, N - 1) {
    num = num - a[i];
    x += (num % mod) * a[i] % mod;
  }
  x = x % mod;

  cout << x << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define PI 3.14159265358979323846264338327950

int main() {
  ll N, mod = 1000000007, x = 0, ans = 0;

  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];

  rep(i, N) {
    for (int j = i + 1; j < N; j++) {
      x += a[i] * a[j] % mod;
      x = x % mod;
    }
    x = x % mod;
  }

  cout << x << endl;
  return 0;
}