alexjercan/bugnet huggingface dataset:
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<ll> a(n);
  vector<ll> sum(n);
  ll b = 1000000007;
  rep(i, n) {
    cin >> a[i];
    if (i == 0)
      sum[i] = a[i];
    else
      sum[i] = sum[i - 1] + a[i];
    sum[i] %= b;
  }

  ll ans = 0;
  for (int i = n - 1; i > 0; i--) {
    ans += (a[i] * sum[i - 1]) % b;
    ans %= b;
  }
  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 b = 1000000007;

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

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

#include <bits/stdc++.h>
#define endl '\\n'
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back
#define ll long long
#define rep(i, n) for (i = 0; i < n; ++i)
#define REP(i, k, n) for (i = k; i <= n; ++i)
#define REPR(i, k, n) for (i = k; i >= n; --i)
using namespace std;
template <class T, class T1> int chkmin(T &x, const T1 &y) {
  return x > y ? x = y, 1 : 0;
}
template <class T, class T1> int chkmax(T &x, const T1 &y) {
  return x < y ? x = y, 1 : 0;
}
const int MAXN = (1 << 20);
bool prime(ll x) {
  if (x == 1)
    return false;
  if (x == 2 || x == 3 || x == 5)
    return true;
  for (ll i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  // freopen("error.txt", "w", stderr);
  freopen("output.txt", "w", stdout);
#endif

  ll T = 1;
  // cin>>T;

  for (ll t = 0; t < T; t++) {
    ll n, ans = 0, mod = 1000000007;
    cin >> n;
    ll sum = 0;
    vector<ll> arr(n);
    for (ll i = 0; i < n; i++) {
      cin >> arr[i];
      sum = (sum + arr[i]) % mod;
    }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back
#define ll long long
#define rep(i, n) for (i = 0; i < n; ++i)
#define REP(i, k, n) for (i = k; i <= n; ++i)
#define REPR(i, k, n) for (i = k; i >= n; --i)
using namespace std;
template <class T, class T1> int chkmin(T &x, const T1 &y) {
  return x > y ? x = y, 1 : 0;
}
template <class T, class T1> int chkmax(T &x, const T1 &y) {
  return x < y ? x = y, 1 : 0;
}
const int MAXN = (1 << 20);
bool prime(ll x) {
  if (x == 1)
    return false;
  if (x == 2 || x == 3 || x == 5)
    return true;
  for (ll i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  // freopen("error.txt", "w", stderr);
  freopen("output.txt", "w", stdout);
#endif

  ll T = 1;
  // cin>>T;

  for (ll t = 0; t < T; t++) {
    ll n, ans = 0, mod = 1000000007;
    cin >> n;
    vector<ll> arr(n);
    for (ll i = 0; i < n; i++)
      cin >> arr[i];
    for (ll i = 0; i < n; i++) {
      for (ll j = i + 1; j < n; ++j)
        ans = (ans + (arr[i] * arr[j]) % mod) % mod;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using s64 = signed long long;
using u64 = unsigned long long;
using f128 = long double;

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

  u64 sum = 0;
  u64 jSum = 0;
  u64 div = pow(10, 9) + 7;
  for (s64 i = n - 2; i >= 0; i--) {
    s64 j = i + 1;
    jSum += a[j];
    jSum %= div;
    sum += a[i] * jSum;
    sum %= div;
  }

  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using s64 = signed long long;
using u64 = unsigned long long;
using f128 = long double;

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

  s64 sum = 0;
  s64 div = pow(10, 9) + 7;
  for (s64 i = 0; i < n; i++) {
    for (s64 j = i + 1; j < n; j++) {
      sum += a[i] * a[j];
      sum %= div;
    }
  }

  cout << sum << endl;

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

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

int func(string s, string t, int i, int j) {
  if (j < 0) {
    return 0;
  } else if (i < 0) {
    return j + 1;
  } else if (t[j] == s[i]) {
    return (func(s, t, i - 1, j - 1));
  } else {
    return (min(func(s, t, i - 1, j - 1) + 1, func(s, t, i - 1, j)));
  }
}
ll gcd(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return (gcd(b % a, a));
}
ll power(ll x, ll y) {
  if (y == 0) {
    return 1;
  }
  ll p = power(x, y / 2);
  p = (p * p) % mod;
  return (y & 1) ? (x * p) % mod : p;
}
ll modInverse(ll a) { return (power(a, mod - 2)); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  scanf("%d", &n);
  ll a[n];
  for (int i = 0; i < n; i++) {
    scanf("%llu", &a[i]);
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum = (sum + a[i]) % mod;
  }
  sum = (sum * sum) % mod;
  ll square_sum = 0;
  for (int j = 0; j < n; j++) {
    square_sum = (square_sum + (a[j] * a[j]) % mod) % mod;
  }
  ll modinv = modInverse(2);
  // printf("%llu\\n",modinv);
  ll ans = (((sum - square_sum + mod) % mod) * modinv) % mod;
  printf("%llu\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

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

int func(string s, string t, int i, int j) {
  if (j < 0) {
    return 0;
  } else if (i < 0) {
    return j + 1;
  } else if (t[j] == s[i]) {
    return (func(s, t, i - 1, j - 1));
  } else {
    return (min(func(s, t, i - 1, j - 1) + 1, func(s, t, i - 1, j)));
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  scanf("%d", &n);
  ll a[n];
  for (int i = 0; i < n; i++) {
    scanf("%llu", &a[i]);
  }
  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] == 0) {
      continue;
    }
    for (int j = i + 1; j < n; j++) {
      ans = (ans + (a[i] * a[j]) % mod) % mod;
    }
  }
  printf("%llu\\n", ans);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

void solve() {
  int N;
  cin >> N;
  int A[N];
  for (int &i : A)
    cin >> i;

  ll ans = 0;
  const int mod = 1e9 + 7;
  int suff[N] = {0};
  suff[N - 1] = A[N - 1];
  for (int i = N - 2; i >= 0; i--)
    suff[i] = (A[i] + suff[i + 1]) % mod;
  for (int i = 0; i < N - 1; i++)
    ans = (ans + (1ll * A[i] * suff[i + 1]) % mod) % mod;

  cout << ans;
}

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

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  // cout << nl;
  // }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

void solve() {
  int N;
  cin >> N;
  int A[N];
  for (int &i : A)
    cin >> i;

  ll ans = 0;
  const int mod = 1e9 + 7;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += (1ll * A[i] * A[j]) % mod;
      if (ans >= mod)
        ans -= mod;
    }
  }

  cout << ans;
}

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

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  // cout << nl;
  // }

  return 0;
}
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;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long long ll;
#define pb push_back
#define pi (acos(-1))
#define ull unsigned long long
#define ld long double
#define all(v) v.begin(), v.end()
/// freopen("input.txt","r",stdin);
/// freopen("output.txt","w",stdout);
ll LCM(ll a, ll b) {
  ll g = __gcd(a, b);
  return (a / g) * b;
}
string numtostr(ll n) {
  ostringstream str1;
  str1 << n;
  return str1.str();
}
ll strtonum(string s) {
  ll x;
  stringstream str1(s);
  str1 >> x;
  return x;
}
#define mx 1000000007
int n, i;
ll s = 0, sum = 0, b = 0;
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  ll a[n];
  for (i = 0; i < n; ++i)
    cin >> a[i];
  for (i = 1; i < n; ++i)
    s += a[i];
  for (i = 0; i < n; ++i) {
    if (i == 0)
      sum = ((sum % mx) + ((s % mx) * (a[i] % mx))) % mx;
    else {
      s -= a[i];
      sum = ((sum % mx) + ((s % mx) * (a[i] % mx))) % mx;
    }
  }
  cout << sum << '\\n';
  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;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long long ll;
#define pb push_back
#define pi (acos(-1))
#define ull unsigned long long
#define ld long double
#define all(v) v.begin(), v.end()
/// freopen("input.txt","r",stdin);
/// freopen("output.txt","w",stdout);
ll LCM(ll a, ll b) {
  ll g = __gcd(a, b);
  return (a / g) * b;
}
string numtostr(ll n) {
  ostringstream str1;
  str1 << n;
  return str1.str();
}
ll strtonum(string s) {
  ll x;
  stringstream str1(s);
  str1 >> x;
  return x;
}
int n, i, j;
ll s = 0, mx = 1e9 + 7;
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  ll a[n];
  for (i = 0; i < n; ++i)
    cin >> a[i];
  for (i = 0; i < n; ++i) {
    for (j = i + 1; j < n; ++j) {
      s = (s % mx + a[i] * a[j] % mx) % mx;
    }
  }
  cout << s << '\\n';
  return 0;
}
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<int64_t> A(N), Sum(N - 1, 0);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  Sum.at(0) = A.at(N - 1) % MOD;
  for (int i = 1; i < N - 1; i++)
    Sum.at(i) = (Sum.at(i - 1) + A.at(N - 1 - i)) % MOD;

  int64_t ans = 0;

  for (int i = 0; i < N - 1; i++) {
    ans += Sum.at(N - 2 - i) * A.at(i) % MOD;
    ans %= MOD;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

  vector<int64_t> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  int64_t ans = 0;

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

  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

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

int md = 1e9 + 7;

void solve() {
  ll n;
  cin >> n;
  vector<ll> v(n);
  for (ll &item : v)
    cin >> item;

  // LOGIC
  vector<ll> sum(n);
  sum[n - 1] = v[n - 1];
  for (int i = n - 2; i >= 0; --i) {
    sum[i] = sum[i + 1] + v[i];
    sum[i] %= md;
  }

  ll ans = 0;
  for (int i = 1; i < n; ++i) {
    ans += v[i - 1] * sum[i];
    ans %= md;
  }
  cout << ans << "\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  while (t--) {
    solve();
  }
}


Error: WA

Fixed submission:

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

const int sz = 1e6 + 1;
vector<bool> primes(sz);
vector<int> g(sz);

void solve() {
  // INPUT
  int n;
  cin >> n;
  set<int> input;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    input.insert(a);
  }

  // LOGIC
  int M = *input.rbegin();
  fill(g.begin(), g.end(), 0);
  fill(primes.begin(), primes.end(), true);
  primes[0] = false;
  primes[1] = false;
  for (int i = 2; i * i <= M; i++) {
    if (primes[i]) {
      for (int j = 2 * i; j <= M; j += i) {
        primes[j] = false;
        if (input.count(j))
          g[i]++;
      }
    }
  }

  bool pairwise = true;
  for (int i = 2; i <= M; ++i) {
    pairwise = pairwise && (g[i] < 2);
  }

  int p = *input.begin();
  for (auto it = next(input.begin()); it != input.end(); ++it) {
    p = __gcd(p, *it);
  }

  if (pairwise)
    cout << "pairwise coprime\\n";
  else if (p == 1)
    cout << "setwise coprime\\n";
  else
    cout << "not coprime\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  while (t--) {
    solve();
  }
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
  int n, ans = 0, sum = 0;
  cin >> n;
  std::vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    ans += a[i] * sum % 1000000007;
    sum += a[i];
    sum %= 1000000007;
    ans %= 1000000007;
  }
  cout << ans << endl;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
  int n, ans = 0;
  cin >> n;
  std::vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += v[i] * v[j];
      ans %= 1000000007;
    }
  }
  cout << ans << endl;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // int t;cin >> t ;while(t--)
  solve();
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int m = 1000000007;
  int n;
  cin >> n;
  vector<long long> v(n);
  vector<long long> pref(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (i == 0)
      pref[0] = v[0] % m;
    else {
      pref[i] = ((pref[i - 1] % m) + (v[i] % m)) % m;
      pref[i] % m;
    }
  }
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    ans += ((v[i] % m) * (pref[i - 1] % m)) % m;
    ans %= m;
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int md = 1000000007;
  int n;
  cin >> n;
  long long ans = 0;
  vector<long long> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x != 0)
      v.push_back(x);
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += ((v[i] % md) * (v[j] % md)) % md;
      ans %= md;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long ans = 0, n, a[210000], sum[210000] = {0};
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    sum[i] = (sum[i - 1] + a[i]) % mod;
  }
  for (long long i = 1; i < n; i++) {
    ans = (ans + ((a[i] % mod) * (sum[n] - sum[i] + mod) % mod) % mod) % mod;
  }
  cout << ans;
}

Error: WA

Fixed submission:

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define M 1010
#define N 1000000
#define LL long long
#pragma GCC optimize(2)
using namespace std;
LL mod = 1e9 + 7, a[N], sum = 0, n, ans = 0;
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += a[i];
  }
  for (int i = 0; i < n; i++) {
    ans -= a[i];
    sum = sum % mod + (ans % mod) * (a[i] % mod) % mod;
    sum %= mod;
  }
  cout << sum % mod;
  return 0;
}
/*
 */


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define M 1010
#define N 1000000
#define LL long long
#pragma GCC optimize(2)
using namespace std;
LL mod = 1e9 + 7, a[N], sum = 0, n;
int main() {
  std::ios::sync_with_stdio(false);
  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 % mod + a[i] * a[j] % mod;
  cout << sum % mod;
  return 0;
}
/*
 */
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include "algorithm"
#include "cmath"
#include "cstdio"
#include "cstring"
#include "iostream"
#include "queue"
#include "stack"
using namespace std;

#define read(x) scanf("%d", &x)
#define readl(x) scanf("%lld", &x)
#define ll long long
#define ull unsigned long long
#define MOD 1000000007

int n;
ll a[200005];
ll sum[200005];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = n; i >= 1; i--) {
    sum[i] = (sum[i + 1] + a[i]) % MOD;
  }
  for (int i = 1; i < n; i++) {
    sum[0] = (sum[0] + a[i] * sum[i + 1]) % MOD;
  }
  printf("%lld\\n", sum[0] % MOD);
  return 0;
}

Error: 0

Fixed submission:

#include "algorithm"
#include "cmath"
#include "cstdio"
#include "cstring"
#include "iostream"
#include "queue"
#include "stack"
using namespace std;

#define read(x) scanf("%d", &x)
#define readl(x) scanf("%lld", &x)
#define ll long long
#define ull unsigned long long
#define MOD 1000000007

int n;
ll a[100005];
ll sum[100005];

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

#include <iostream>
#define MAXN 200500

using namespace std;

int a[MAXN];
long long b[MAXN];
const long long m = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  b[0] = a[n - 1];
  for (int i = 1; i < n - 1; ++i) {
    b[i] = b[i - 1] + a[n - 1 - i];
    b[i] %= m;
  }
  int sum = 0;
  for (int i = 0; i < n - 1; ++i) {
    sum += (int)((a[i] * b[n - 2 - i] % m));
    sum %= m;
  }
  cout << sum;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#define MAXN 200500

using namespace std;

__int128 a[MAXN];
const long long m = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    a[i] = x;
  }
  int sum = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      sum += (int)((a[i] * a[j]) % m);
      sum %= m;
    }
  }
  cout << (long long)sum;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  long long ans = 0, sum = 0;
  int mod = 1000000000 + 7;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
    sum += a.at(i);
    sum %= mod;
  }
  for (int i = 0; i < N - 1; i++) {
    sum -= a.at(i);
    if (sum < 0)
      sum += mod;
    ans += sum * a.at(i);
    ans %= mod;
  }
  cout << ans << endl;
}
/*

*/

Error: TLE

Fixed submission:

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

int main() {
  int N, h = 1000000000 + 7;
  long sum = 0;
  cin >> N;
  vector<long> 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++) {
      sum += a.at(i) * a.at(j);
      sum = sum % h;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N, i, a = 0;
  cin >> N;
  long long int A[N], P[N + 1];
  P[N] = 0;
  for (i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (i = N - 1; i >= 0; i--) {
    P[i] = (P[i + 1] + A[i]) % 1000000007;
    A[i] = A[i] % 1000000007;
  }
  for (i = 0; i < N; i++) {
    a = (a + ((A[i] * (P[i + 1]))) % 1000000007) % 1000000007;
  }
  cout << a % 1000000007 << endl;
}

Error: TLE

Fixed submission:

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using ull = unsigned long long;
using ll = long long;
using namespace std;

const ll M = 1000000007;

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

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

  vector<ll> b(n, 0);
  b[n - 1] = a[n - 1];

  for (int bi = 1; bi < n; ++bi) {
    b[(n - 1) - bi] = (b[(n - 1) - bi + 1] + a[(n - 1) - bi]) % M;
  }
  // for(int bi = 0; bi < n; ++ bi){
  //     cout << b[bi] << " ";
  // }
  // cout << endl;

  ll ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    ans += (a[i] * b[i + 1]) % M;
    // cout << a[i] << "*" << b[i+1] << endl;
  }

  cout << ans % M << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using ull = unsigned long long;
using ll = long long;
using namespace std;

const ll M = 1000000007;

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

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

  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 %= M;
    }
  }

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

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

int main() {

  int N;
  unsigned long long int ans = 0;
  std::cin >> N;

  std::vector<unsigned long long int> list(N, 0);

  unsigned long long int sum = 0;

  for (int i = 0; i < N; i++) {
    std::cin >> list[i];
    sum += list[i];
  }

  for (int i = 0; i < N - 1; i++) {
    ans += ((list[i] % 1000000007) * ((sum -= list[i]) % 1000000007));
    ans %= 1000000007;
  }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {

  std::vector<unsigned long long int> list;

  int N;
  unsigned long long int ans = 0;
  std::cin >> N;
  list.reserve(N);

  for (int i = 0; i < N; i++) {
    std::cin >> list[i];
    list[i] %= 1000000007;
  }

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

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