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

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

int n;
ll p = 1e9 + 7, a[MAXN], sum[MAXN], ans = 0;

inline int read() {
  char c = getchar();
  int res = 0;
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  return res;
}

int main() {

  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = n; i >= 1; i--)
    sum[i] = (sum[i + 1] + a[i]) % p;
  for (int i = 1; i <= n; i++) {
    ans = (ans + a[i] * sum[i + 1]) % p;
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
const int N = 100010, p = 1e9 + 7;
typedef long long ll;
ll sum[N], a[N], n, ans;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  for (int i = 1; i <= n; i++)
    ans = (ans + a[i] * (sum[n] - sum[i])) % p;
  printf("%lld", ans);
  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, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define ALL(v) (v).begin(), (v).end()
ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  VL a(N);
  ll sum = 0;
  rep(i, 0, N) {
    cin >> a[i];
    sum += a[i];
  }

  ll ans = 0;
  rep(i, 0, N - 1) {
    sum = sum - a[i];
    // cout << sum << endl;
    ans += (a[i] % MOD) * (sum % MOD);
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define ALL(v) (v).begin(), (v).end()
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

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

#include <bits/stdc++.h>
#define INF 2147483647
#define ll long long
using namespace std;
const int Max = 2e5 + 20;
const int Maxn = 1e7 + 20;
const int mod = 1e9 + 7;
inline int read() {
  int f = 1, x = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-')
      f = -1;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return x * f;
}
ll a[Max];
ll sum[Max];
int main() {

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 2147483647
#define ll long long
using namespace std;
const int Max = 2e5 + 20;
const int Maxn = 1e7 + 20;
const int mod = 1e9 + 7;
inline int read() {
  int f = 1, x = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-')
      f = -1;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return x * f;
}
ll a[Max];
int main() {

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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define pii pair<int, int>
#define fi first
#define se second
#define int long long
#define pb push_back
#define mod 1000000007
#define pll pair<long long, long long>
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long power(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
// find_by_order() ->  iterator to the k-th largest element (counting from zero)
// order_of_key() ->    the number  items in a set that are strictly smaller
// than our item aka index in set
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  //(a*b)%m=((a%m)*(b%m))%m
  int x = 0;
  for (int i = 0; i < n; i++) {
    x += a[i];
    x %= mod;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    x -= a[i];
    if (x < 0)
      x += mod;
    ans += (a[i] * x);
    ans %= mod;
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define pii pair<int, int>
#define fi first
#define se second
#define int long long
#define pb push_back
#define mod 1000000007
#define pll pair<long long, long long>
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long power(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
// find_by_order() ->  iterator to the k-th largest element (counting from zero)
// order_of_key() ->    the number  items in a set that are strictly smaller
// than our item aka index in set
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  //(a*b)%m=((a%m)*(b%m))%m
  int x = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int k = ((a[i] % mod) * (a[j] % mod)) % mod;
      x += (k);
      x %= mod;
    }
  }
  cout << x % mod;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pld pair<long double, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define ff first
#define ss second
#define SZ(x) ((int)(x).size())
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int M = 1e9 + 7;
const int MAXN = 2e5 + 100;
int N;
ll a[MAXN];

void read() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
}

void solve() {
  read();
  vector<ll> prefix(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    prefix[i] = (prefix[i - 1] + a[i]) % M;
  }
  ll ans = 0;
  for (int i = N; i > 0; i--) {
    ans = (ans + ((prefix[i - 1] * a[i]) % M)) % M;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pld pair<long double, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define ff first
#define ss second
#define SZ(x) ((int)(x).size())
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int M = 1e9 + 7;
const int MAXN = 1e5 + 100;
int N;
ll a[MAXN];

void read() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
}

void solve() {
  read();
  vector<ll> prefix(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    prefix[i] = prefix[i - 1] + a[i];
  }
  ll ans = 0;
  for (int i = N; i > 0; i--) {
    ans = (ans + ((prefix[i - 1] * a[i]) % M)) % M;
  }
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define mod 1000000007

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n - 1);
  rep(i, n) { cin >> a[i]; }

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

  int i = 1;
  for (int j = 0; j < n - 1; j++) {
    b[j] = sum;
    sum -= a[i++];
  }

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

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define mod 1000000007

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) { 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];
      ans %= mod;
    }
  }

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

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#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 mod = 1'000'000'007;
  int ans = 0;
  int x = 0;
  rep(i, n) {
    ans = (ans + (ll)a[i] * x) % mod;
    x = (x + a[i]) % mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#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 ans = 0;

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

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

// 負の数にも対応した mod (a = -11 とかでも OK)
inline ll mod(ll a, ll m) { return (a % m + m) % m; }

int main() {
  ll MOD = 1000000007;
  int N;
  cin >> N;
  vector<ll> A(N);
  ll wa = 0;
  rep(i, N) {
    cin >> A.at(i);
    wa += A[i];
    wa %= MOD;
  }
  ll res = 0;
  rep(i, N - 1) {
    wa -= A[i];
    wa = (wa % MOD + MOD) % MOD;
    res += A[i] * wa % MOD;
    res %= MOD;
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  ll MOD = 1000000007;
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  sort(A.begin(), A.end());
  ll res = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      res += A[i] * A[j] % MOD;
      res %= MOD;
    }
  }
  cout << res << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void solve() {
  ll n;
  cin >> n;
  vector<ll> A(n);
  for (ll i = 0; i < n; ++i)
    cin >> A[i];
  vector<ll> newarr(n);
  ll sum = 0, temp;

  ll res = 0;
  newarr[0] = A[n - 1] % M;
  // sort(A.begin(),A.end());
  for (ll i = 1; i < n; i++) {
    newarr[i] = (newarr[i - 1] + A[n - i - 1]) % M;
    newarr[i] %= M;
  }

  for (ll i = 0; i < n - 1; i++) {
    sum += (A[i] * newarr[n - i - 2]) % M;
    sum %= M;
  }

  cout << sum % M;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int q;
  q = 1;
  // cin>>q;
  while (q--)
    solve();

  return 0;
}


Error: TLE

Fixed submission:

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

void solve() {
  ll n;
  cin >> n;
  ll A[n];
  for (ll i = 0; i < n; ++i)
    cin >> A[i];
  ll sum = 0, temp;

  for (ll i = 0; i < n - 1; i++) {
    for (ll j = i + 1; j < n; ++j) {
      temp = A[i] * A[j];
      temp %= M;
      sum += temp;
      sum %= M;
    }
  }

  cout << sum % M;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int q;
  q = 1;
  // cin>>q;
  while (q--)
    solve();

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

#include <bits/stdc++.h>
#define lg long long int
#define loop(i, s, e) for (lg i = s; i <= e; i++)
#define iloop(i, s, e) for (lg i = e; i >= s; i--)
#define pb push_back
#define mp make_pair
#define mod 1000000007
using namespace std;

lg min(lg a, lg b) { return a < b ? a : b; }
lg max(lg a, lg b) { return a > b ? a : b; }
lg lcm(lg a, lg b) { return (a * b) / __gcd(a, b); }
int main() {
  lg n;
  cin >> n;
  vector<lg> a(n, 0);
  vector<lg> b(n, 0);

  for (lg i = 0; i < n; i++)
    cin >> a[i];

  b[n - 1] = a[n - 1];
  for (lg i = n - 2; i >= 0; i--)
    b[i] = (a[i] + b[i + 1]) % mod;
  lg ans = 0;
  for (lg i = 0; i < n - 1; i++)
    ans = (ans + ((a[i] * b[i + 1]) % mod)) % mod;
  cout << ans % mod << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lg long long int
#define loop(i, s, e) for (lg i = s; i <= e; i++)
#define iloop(i, s, e) for (lg i = e; i >= s; i--)
#define pb push_back
#define mp make_pair
#define mod 1000000007
using namespace std;

lg min(lg a, lg b) { return a < b ? a : b; }
lg max(lg a, lg b) { return a > b ? a : b; }
lg lcm(lg a, lg b) { return (a * b) / __gcd(a, b); }
int main() {
  lg n;
  cin >> n;
  vector<lg> a(n, 0);

  for (lg i = 0; i < n; i++)
    cin >> a[i];

  lg ans = 0;
  for (lg i = 0; i < n; i++)
    for (lg j = i + 1; j < n; j++)
      ans = (ans + ((a[i] * a[j]) % mod)) % mod;
  cout << ans % mod << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int num;
  std::cin >> num;
  long A[num];
  std::vector<long long> B(num + 1, 0);
  for (int i = 0; i < num; i++) {
    std::cin >> A[i];
    B[i + 1] = B[i] + A[i];
  }

  int mod = 1000000007;
  long ans = 0;
  for (int i = 0; i < num; i++) {
    long long sum = (B[num] - B[i + 1]) % mod;
    // std::cout << sum_all << std::endl;
    ans += sum * A[i];
    ans %= mod;
  }

  // std::cout << (long)141421356*17320508 << std::endl;
  std::cout << ans << std::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 <cmath>
#include <iostream>
#include <vector>

int main() {
  int num;
  std::cin >> num;
  long A[num];
  std::vector<long long> B(num, 0);
  for (int i = 0; i < num; i++) {
    std::cin >> A[i];
    B[i + 1] = B[i] + A[i];
  }

  int mod = 1000000007;
  long ans = 0;
  for (int i = 0; i < num; i++) {
    long long sum = (B[num] - B[i + 1]) % mod;
    // std::cout << sum_all << std::endl;
    ans += sum * A[i];
    ans %= mod;
  }

  // std::cout << (long)141421356*17320508 << std::endl;
  std::cout << ans << std::endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define OM                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
#define endl '\\n'
#define pi 3.14159265358979323846

typedef long long ll;

/*LETS BEGIN WITH THE NAME OF MAHADEV*/

/*ll mul(ll a, ll b, ll m){
        ll res = 0;
        a = a % m;
        while(b){
                if(b & 1)
                        res = (res + a) % m;
                a = (a + a) % m;
                b >>= 1;
        }
        return res;
}
*/
int main() {
  OM;
  int n;
  cin >> n;
  const ll m = 1e9 + 7;
  vector<ll> v(n), pref(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    pref[i] = i ? (pref[i - 1] + v[i]) % m : v[i] % m;
  }
  ll answer = 0;
  for (int i = 0; i < n; ++i) {
    answer = (answer + (v[i] * (pref[n - 1] - pref[i] + m) % m) % m) % m;
  }
  cout << answer << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define OM                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
#define endl '\\n'
#define pi 3.14159265358979323846

typedef long long ll;

/*LETS BEGIN WITH THE NAME OF MAHADEV*/

/*ll mul(ll a, ll b, ll m){
        ll res = 0;
        a = a % m;
        while(b){
                if(b & 1)
                        res = (res + a) % m;
                a = (a + a) % m;
                b >>= 1;
        }
        return res;
}
*/
int main() {
  OM;
  int n;
  cin >> n;
  const ll m = 1e9 + 7;
  vector<ll> v(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  ll answer = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      answer = (answer + (((v[i] % m) * (v[j] % m)) % m)) % m;
    }
  }

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

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
typedef long long ll;
const ll mod = (1e9 + 7);
int main() {
  IOS;
  int n;
  cin >> n;
  vector<ll> a(n);
  ll sol = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sol += a[i];
    if (sol > mod)
      sol %= mod;
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sol -= a[i];
    if (sol < 0)
      sol += mod;
    sum += ll(a[i] * (sol)) % mod;
  }

  cout << sum % mod << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
typedef long long ll;
const ll mod = (1e9 + 7);
int main() {
  IOS;
  int n;
  cin >> n;
  vector<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 += ll(a[i] * a[j]) % mod;
    }
  }
  sum %= mod;
  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++)
#define all(v) v.begin(), v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  ll n;
  cin >> n;
  ll sum = 0;
  ll ans = 0;
  vector<ll> a(n);
  cin >> a[0];
  sum += a[0];
  for (int i = 1; i < n; i++) {
    cin >> a[i];
    ans += sum * a[i];
    ans = ans % (INF + 7);
    sum += a[i];
    sum = sum % (INF + 7);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

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

#include <bits/stdc++.h>

#define PI 3.14159265359
#define rep(i, a, n) for (int i = a; i < (int)n; ++i)
#define SZ(x) ((int)(x).size()) // size() unsigned -> int
#define descSort(a) sort(a.begin(), a.end(), std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;

int main(void) {
  ll n;
  cin >> n;
  vector<ll> a(n), l(n + 1);
  l[0] = 0;
  ll sum = 0;
  rep(i, 0, n) {
    cin >> a[i];
    sum += a[i] % INF;
    l[i + 1] = l[i] + a[i];
  }

  ll ans = 0;
  rep(i, 0, n) {
    ll sum = (l[n] - l[i + 1]) % INF;
    ans += a[i] * sum;
    ans %= INF;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define PI 3.14159265359
#define rep(i, a, n) for (int i = a; i < (int)n; ++i)
#define SZ(x) ((int)(x).size()) // size() unsigned -> int
#define descSort(a) sort(a.begin(), a.end(), std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;

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

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

#include <cstdio>
#include <iostream>
using namespace std;
const int SIZE = 200000 + 10, mod = 1000000007;
const int p = 500000004;
long long n, A[SIZE], s, t, ans;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%lld", &A[i]);
  s = t = 0;
  for (int i = 0; i < n; ++i) {
    s = (s + A[i]) % mod;
    t = (t + A[i] * A[i]) % mod;
  }
  s = s * s % mod;
  ans = (s - t + mod) % mod;
  printf("%d\\n", (long long)(ans * p) % mod);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
const int SIZE = 200000 + 10, mod = 1000000007;
long long n, A[SIZE], ans;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%lld", &A[i]);
  ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      ans = (long long)(ans + A[i] * A[j]) % mod;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

int main(int /*argc*/, char ** /*argv*/) {
  int N;
  std::cin >> N;
  std::vector<uint64_t> A(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }
  uint64_t ans = 0;
  uint64_t AJ = 0;
  for (int i = 0; i < N; ++i) {
    AJ += A[i];
  }
  for (int i = 0; i < N; ++i) {
    AJ -= A[i];
    ans = (ans + A[i] * (AJ % 1000000007ULL)) % 1000000007ULL;
  }

  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

int main(int /*argc*/, char ** /*argv*/) {
  int N;
  std::cin >> N;
  std::vector<uint64_t> A(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }
  uint64_t ans = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      ans = (ans + A[i] * A[j]) % 1000000007ULL;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}