alexjercan/bugnet huggingface dataset:
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

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

// ------------------------------------
// 1 is true
// 0 is false
// ------------------------------------

ll binexp(ll x, ll n, ll m) {
  x %= m;
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    n >>= 1;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  const ll k = 1000000007;
  ll sumai = 0, sumaisq = 0, minvtwo = binexp(2, (k - 2), k);
  for (int i = 0; i < n; i++) {
    sumai += a[i] % k;
    sumaisq += ((a[i] % k) * (a[i] % k)) % k;
    sumai = sumai % k;
    sumaisq = sumaisq % k;
  }

  cout << (minvtwo * ((sumai * sumai - sumaisq) % k)) % k << endl;
}


Error: 0

Fixed submission:

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

// ------------------------------------
// 1 is true
// 0 is false
// ------------------------------------

ll binexp(ll x, ll n, ll m) {
  x %= m;
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    n >>= 1;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  const ll k = 1000000007;
  ll sumai = 0, sumaisq = 0, minvtwo = binexp(2, (k - 2), k);
  for (int i = 0; i < n; i++) {
    sumai += a[i] % k;
    sumaisq += ((a[i] % k) * (a[i] % k)) % k;
    sumai = sumai % k;
    sumaisq = sumaisq % k;
  }

  cout << (minvtwo * ((sumai * sumai - sumaisq) % k)) % k << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded


// Problem : C - Sum of product of pairs
// Contest : AtCoder - AtCoder Beginner Contest 177
// URL : https://atcoder.jp/contests/abc177/tasks/abc177_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define fr(i, a, b) for (int i = a; i < b; i++)
#define frr(i, a, b) for (int i = a; i >= b; i--)
// vector
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define pp pop_back
// pair
#define pi pair<int, int>
#define mk make_pair
#define ff first
#define ss second
// set
#define si set<int>
#define sit set<int>::iterator
using namespace std;
ll mod = 1000000007;
int main() {
  /*
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif
  */
  fast;
  ll n;
  cin >> n;
  ll a[n];
  ll sum[n];
  fr(i, 0, n) {
    cin >> a[i];
    if (i == 0)
      sum[i] = a[i];
    else
      sum[i] = sum[i - 1] + a[i];
  }
  ll ans = 0, k = n - 1, j = 0;
  fr(i, 0, n - 1) {
    ans = (ans + (a[i] * ((sum[k] - sum[j]) % mod) % mod)) % mod;
    j++;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:


// Problem : C - Sum of product of pairs
// Contest : AtCoder - AtCoder Beginner Contest 177
// URL : https://atcoder.jp/contests/abc177/tasks/abc177_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define fr(i, a, b) for (int i = a; i < b; i++)
#define frr(i, a, b) for (int i = a; i >= b; i--)
// vector
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define pp pop_back
// pair
#define pi pair<int, int>
#define mk make_pair
#define ff first
#define ss second
// set
#define si set<int>
#define sit set<int>::iterator
using namespace std;
ll mod = 1000000007;
int main() {
  /*
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif
  */
  fast;
  ll n;
  cin >> n;
  ll a[n];
  fr(i, 0, n) cin >> a[i];
  ll ans = 0;
  fr(i, 0, n) {
    fr(j, i + 1, n) { ans = (ans + ((a[i] * a[j]) % mod)) % mod; }
  }
  cout << ans;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n;
  cin >> n;
  ll a[n];
  int mod = 1000000007;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll b[n + 1] = {};
  for (int i = 1; i < n + 1; i++) {
    b[i] = b[i - 1] + a[i - 1];
    // b[i] %= mod;
  }

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

  cout << sum;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  ll a[n];
  int mod = 1000000007;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += a[i] * a[j] % mod;
      sum %= mod;
    }
  }
  cout << sum;
}
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);
  int mod = 1000000007;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
    sum %= mod;
  }

  long 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 <bits/stdc++.h>
using namespace std;
#define ll long long int
#define loop(i, a, n) for (int i = a; i < n; i++)
const unsigned int M = 1000000007;

int main() {
  ll n;
  cin >> n;
  ll *a = new ll[n];
  loop(i, 0, n) cin >> a[i];

  ll ans = 0;
  loop(i, 0, n - 1) {
    loop(j, i + 1, n) {
      ll pro = (a[i] * a[j]) % M;
      ans += pro % M;
      ans = ans % M;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fi first
#define sec second
#define pr priority_queue
ll mod = 1000000007;
int main() {
  int n;
  cin >> n;
  ll a[n], q = 0;
  ll s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s = (s + a[i] + mod) % mod;
  }
  for (int i = 0; i < n; i++) {
    ll d = (s - a[i] + mod) % mod;
    ll k = (a[i] * d) % mod;
    q = (q + k) % mod;
    s = (s - a[i] + mod) % mod;
  }
  cout << q;
}


Error: TLE

Fixed submission:

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

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;
  long long int mod = 1000000007;
  long long a[n], s = 0;
  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++) {
      long long int p;
      p = moduloMultiplication(a[i], a[j], mod);
      s = s + p;
    }
  }
  s = s % mod;
  cout << s;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int long long
#define INT int
#define MOD 1000000007

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
template <typename T> void print(T x) { std::cout << x << '\\n'; }
typedef long long ll;
const long long INF = 1LL << 60;

signed main() {

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

  int ans = 0;
  int s = 0;
  rep1(i, a.size()) {
    s = s + a[i];
    s = s % MOD;
  }

  // cout << s << endl;

  rep(i, a.size() - 1) {
    int temp = a[i] * s % MOD;
    ans = (ans + temp) % MOD;
    s = (s + MOD - a[i + 1]) % MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define int long long
#define INT int
#define MOD 1000000007

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
template <typename T> void print(T x) { std::cout << x << '\\n'; }
typedef long long ll;
const long long INF = 1LL << 60;

signed main() {

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

  int ans = 0;

  rep(i, a.size() - 1) {
    for (int j = i + 1; j < a.size(); j++) {
      int temp = (a[i] * a[j]) % MOD;
      ans += temp;
      ans = 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 < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  rep(i, n) cin >> A[i];
  const int m = 1000000007;
  ll ans = 0;
  ll x = 0;
  rep(i, n) {
    ans = (ans + A[i] * x) % m;
    x = (x + A[i]) % m;
  }
  ans = ans % m;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  ll m = 1000000007;

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

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

const unsigned int M = 1000000007;
int N;

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

  long long sum = 0;
  long long suffix = A[N - 1];
  for (int i = N - 2; i > -1; i--) {
    sum += (A[i] * suffix);
    sum %= M;
    suffix += A[i];
    suffix %= M;
  }

  cout << sum << endl;
}


Error: TLE

Fixed submission:

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

const unsigned int M = 1000000007;
int N;

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

  long long sum = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      long long add = A[i] * A[j];
      sum += add;
      sum = sum % M;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

#define MOD 1000000007
#define ll long long

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 m) {
  ll x, y; // used in extended GCD algorithm
  ll g = gcdExtended(b, m, &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 % m + m) % m;
}

int main() {
  int n;

  cin >> n;
  ll a[n];
  ll array_sum = 0, individual_square_sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    array_sum += a[i];
    array_sum %= MOD;

    individual_square_sum += (a[i]) * (a[i]);
    individual_square_sum %= MOD;
  }

  ll array_sum_square = (array_sum) * (array_sum);

  ll ac = (array_sum_square - individual_square_sum) % MOD;
  ll inv = modInverse(2, MOD);

  cout << ((inv * ac) % MOD) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;

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

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

  cout << total << 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 < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll MOD = 1e9 + 7;

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

Error: TLE

Fixed submission:

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

const ll MOD = 1e9 + 7;

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

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

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

  int i, k;
  ll N;
  ll ans = 0;
  int ctr = 0;
  const long long MOD = 1e9 + 7;

  cin >> N;

  vector<ll> gv(N);
  vector<ll> s(N + 1, 0);

  rep(i, N) cin >> gv[i];
  for (i = N - 1; i > 0; i--)
    s[i - 1] = (gv[i] + s[i]) % MOD;

  for (i = 0; i < N - 1; i++) {
    ans += (gv[i] * s[i]) % MOD;
  }

  cout << ans % MOD << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  int i, k;
  ll N;
  ll ans = 0;
  int ctr = 0;
  const long long MOD = 1e9 + 7;

  cin >> N;

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

  for (i = 0; i < N - 1; i++) {
    for (k = i + 1; k < N; k++) {
      ans += ((gv[i] % MOD) * (gv[k] % MOD)) % MOD;
    }
  }

  cout << ans % MOD << endl;

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

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

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

  ll a[n];
  ll sa = 0;
  rep(i, n) {
    ll A = 0;
    cin >> A;
    a[i] = A;
    sa += A;
    sa = sa % LARGE;
  }

  ll sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sa -= a[i];
    if (sa < 0)
      sa += LARGE;
    ll d = (sa * a[i]) % LARGE;
    sum += d;
    sum = sum % LARGE;
  }

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


Error: TLE

Fixed submission:

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

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

  ll a[n];
  rep(i, n) cin >> a[i];

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum = sum % LARGE;
      ll v = a[i] % LARGE;
      ll w = a[j] % LARGE;
      ll x = (v * w) % LARGE;
      sum += x;
      sum = sum % LARGE;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

typedef pair<int, int> P;

int main(void) {
  int N;
  cin >> N;
  vector<ll> vec(N);
  ll ans = 0;
  ll X = 1000000007;
  ll fix = 0;
  for (int k = 0; k < N; k++) {
    cin >> vec[k];
  }
  for (int m = 0; m < N; m++) {
    fix += vec[m];
    fix %= X;
  }
  for (int m = 0; m < N; m++) {
    ll preans = 0;
    fix -= vec[m];
    if (fix < 0)
      fix += X;
    preans = vec[m] * fix;
    preans %= X;
    ans += preans;
    ans %= X;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

typedef pair<int, int> P;

int main(void) {
  int N;
  cin >> N;
  vector<ll> vec(N);
  ll ans = 0;
  ll X = 1000000007;
  for (int k = 0; k < N; k++) {
    cin >> vec[k];
  }
  for (int m = 0; m < N; m++) {
    for (int k = m + 1; k < N; k++) {
      ll w = vec[m] * vec[k];
      w %= X;
      // cout <<w<<endl;
      ans += w;
      ans %= X;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long
#define modu 1000000007

using namespace std;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  int n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ps[n];
  ll last = 0;
  int o = 0;
  for (int i = n - 1; i >= 0; i--) {
    ll now = last + a[i];
    ps[o] = now;
    last = now;
    o++;
  }
  reverse(ps, ps + n);

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    ll psum = (ps[i] - a[i]) % modu;
    ll psum2 = (psum * a[i]) % modu;
    sum = (sum + psum2) % modu;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long

using namespace std;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int 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++)
    for (int j = i + 1; j < n; j++) {
      sum += a[i] * a[j];
      while (sum >= 1000000007)
        sum -= 1000000007;
      // sum = (sum + (a[i] * a[j]))%1000000007;
    }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int const mod = 1e9 + 7;
signed main(void) {
  int n;
  cin >> n;
  ll a[n];
  vector<ll> sum(n + 1, 0);
  ll ans;

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


Error: TLE

Fixed submission:

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

int const mod = 1e9 + 7;
signed main(void) {
  int n;
  cin >> n;
  ll a[n];
  ll sum;

  sum = 0;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    for (int j = i + 1; j < n; 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 <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  const long long int mod = 1000000007;
  int n;
  cin >> n;

  std::vector<long long int> v;
  std::vector<long long int> vv(n);
  for (int i = 0; i < n; i++) {
    long long int tmp;
    cin >> tmp;
    v.push_back(tmp);
  }

  vv[n - 1] = 0;
  // cout << vv[n-1] << endl;
  for (int i = n - 2; i >= 0; i--) {
    vv[i] = (vv[i + 1] % mod + v[i + 1]);
    // cout << vv[i] << endl;
  }
  long long int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum += (v[i] * vv[i] % mod);
    // cout << v[i] << ":" << vv[i] << endl;
  }

  cout << sum % mod << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  const long long int mod = 1000000007;
  int n;
  cin >> n;

  std::vector<long long int> v;
  for (int i = 0; i < n; i++) {
    long long int tmp;
    cin >> tmp;
    v.push_back(tmp);
  }

  // long long int sum = 0;
  __int128 sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += (__int128)(v[i] * v[j]) % (__int128)mod;
      // sum = sum % mod;
      // cout << sum << endl;
    }
  }
  long long int ans = sum % mod;

  cout << ans << endl;

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

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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define intll int long long
const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
  intll n;
  cin >> n;
  vector<intll> v(n);
  intll total = 0;
  rep(i, n) {
    cin >> v.at(i);
    total += v.at(i);
  }
  intll ans = 0;

  for (int i = 0; i < n; i++) {
    total -= v.at(i);
    ans += (v.at(i) % MOD) * (total % MOD) % MOD;
    ans %= MOD;
  }

  cout << ans;
}


Error: TLE

Fixed submission:

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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define intll int long long
const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
  intll n;
  cin >> n;
  vector<intll> v(n);
  rep(i, n) cin >> v.at(i);
  intll ans = 0;

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += (v.at(i) % MOD) * (v.at(j) % MOD) % MOD;
      ans %= MOD;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
signed main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  int ans = 0;
  int help[n];
  help[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--) {
    help[i] = help[i + 1] + arr[i + 1];
    help[i] %= 1000000007;
  }
  for (int i = 0; i < n - 1; i++) {
    int x = (arr[i] * help[i]) % 1000000007;
    ans += x;
    ans %= 1000000007;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
signed main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int x = (arr[i] * arr[j]) % 1000000007;
      ans += x;
      ans %= 1000000007;
    }
  }
  cout << ans;
  return 0;
}