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

#include <bits/stdc++.h>

using namespace std;

#define Fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define pb push_back
#define pf push_front
#define pp pop
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define S second
#define F first
typedef long long ll;
const ll mod = 1e9 + 7;
const ll N = 1e6 + 5;
ll inf = 1e18;

ll a[N];
ll sm[N];

int main() {

  Fast;

  ll n;
  cin >> n;

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

  sm[n - 1] = a[n - 1];

  for (int i = n - 2; i >= 0; i--) {
    sm[i] = a[i] + sm[i + 1];
    sm[i] %= mod;
  }

  ll ans = 0;

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

  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define Fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define pb push_back
#define pf push_front
#define pp pop
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define S second
#define F first
typedef long long ll;
const ll mod = 1e9 + 7;
const ll N = 1e6 + 5;
ll inf = 1e18;

ll a[N];

int main() {

  Fast;

  ll n;
  cin >> n;

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

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

  cout << ans;

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

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

int main() {
  ll N, sum = 0, Nsum = 0;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);
    if (i >= 1) {
      sum += ((Nsum % 1000000007) * (A.at(i) % 1000000007)) % 1000000007;
      sum %= 1000000007;
    }
    Nsum += A.at(i);
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll N, sum = 0;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++)
    cin >> A.at(i);
  for (ll i = 0; i < N; i++) {
    for (ll 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: Time Limit Exceeded

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

#define MOD 1000000007

int main() {
  int n;
  cin >> n;
  long long ans = 0, tmp = 0;
  vector<long long> a(n), b(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i + 1] = b[i] + a[i];
  }
  for (int i = 1; i < n + 1; i++) {
    tmp = (b[n] - b[i]) % MOD * a[i - 1] % MOD;
    ans = (ans + tmp) % MOD;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

#define MOD 1000000007

int main() {
  int n;
  cin >> n;
  long long ans = 0, tmp;
  vector<long long> a(n), b(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i + 1] = b[i] + a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      tmp = a[i] * a[j] % MOD;
      ans = (ans + tmp) % MOD;
    }
  }
  cout << ans << 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 ll = long long;
using P = pair<int, int>;
const int MAX = 1000;
const int INF = 1e9;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
#define PI 3.14159265358979323846264338327950L

int main() {
  int N;
  cin >> N;
  vector<unsigned long long> v;
  vector<unsigned long long> w;
  rep(i, N) {
    int temp;
    cin >> temp;
    v.push_back(temp);
    if (i == 0)
      w.push_back(temp);
    else
      w.push_back(temp + w[i - 1]);
  }
  unsigned long long ans = 0;
  unsigned long long max = 1e9 + 7;
  for (int ii = 0; ii < N; ii++) {
    unsigned long long temp = w[N - 1] - w[ii];
    unsigned long long temp2;
    temp2 = (ans % max) + (v[ii] % max) * (temp % max);
    ans = temp2 % max;
  }
  cout << ans << endl;
}

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 int MAX = 1000;
const int INF = 1e9;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
#define PI 3.14159265358979323846264338327950L

int main() {
  int N;
  cin >> N;
  vector<unsigned long long> v;
  rep(i, N) {
    int temp;
    cin >> temp;
    v.push_back(temp);
  }
  unsigned long long ans = 0;
  unsigned long long max = 1e9 + 7;
  for (int ii = 0; ii < N; ii++) {
    for (int jj = ii + 1; jj < N; jj++) {
      if (ans + v[ii] * v[jj] >= max) {
        ans = (ans + v[ii] * v[jj]) % max;
      } else {
        ans += v[ii] * v[jj];
      }
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int N;
  std::cin >> N;
  long long sumA = 0, ans = 0;
  std::vector<long long> A(N, 0);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
    sumA += A[i];
  }
  long long modN = 1000000007;

  for (int i = 0; i < N - 1; ++i) {
    sumA -= A[i];
    ans += A[i] * (sumA % modN);
    ans %= modN;
  }
  std::cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int N;
  std::cin >> N;
  std::vector<long long> A(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }
  long long ans = 0;

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

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

int main(void) {
  const ll INF = 1e9 + 7;
  ll N;
  cin >> N;
  vector<ll> A;
  vector<ll> sum;
  sum.push_back(0);
  ll ans = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    A.push_back(a);
    ll b = (sum[i] + a);
    // b%=INF;
    sum.push_back(b);
  }

  for (ll i = 0; i < N - 1; i++) {
    ll x = (sum[N] - sum[i + 1]) % INF;
    ans += (A[i] * x) % INF;
    ans = (ans + INF) % INF;
  }
  ans = (ans + INF) % INF;
  cout << ans << endl;
}

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;
typedef pair<int, int> P;

int main(void) {
  const ll INF = 1e9 + 7;
  vector<ll> A;
  ll N;
  cin >> N;
  ll ans = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    A.push_back(a);
  }

  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      ll x = A[i] % INF * A[j] % INF;
      ans += x;
      ans %= INF;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M_PI 3.14159265358979323846

const int M = 1e9 + 7;
long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }

void solve() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> pref(n + 1);
  for (int i = n - 1; i >= 0; i--) {
    pref[i] = pref[i + 1] + a[i];
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = add(ans, mul(a[i], pref[i + 1]));
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  //        freopen("timber_input.txt", "r", stdin);
  //        freopen("timber_output.txt", "w", stdout);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    //            cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}

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;
const int M = 1e9 + 7;
long long mod(long long x) { return ((x % M) + M) % M; }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }

int main() {
  int n;
  vector<long long> arr(n);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  vector<long long> pref(n + 1);
  for (int i = n - 1; i >= 0; i--) {
    pref[i] = pref[i + 1] + arr[i];
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans = add(ans, mul(arr[i], pref[i + 1]));
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

using ll = long long;

int addm(int a, int b) { return ((ll)a + b) % MOD; }

int mulm(int a, int b) { return ((ll)a * b) % MOD; }

int main() {
  int n;
  cin >> n;
  vector<int> arr(n), pref(n);
  for (auto &x : arr)
    cin >> x;
  pref[0] = arr[0];
  for (int i = 1; i < n; i++) {
    pref[i] = addm(pref[i - 1], arr[i]);
  }
  int sum = 0;
  for (int i = 1; i < n; i++) {
    sum = addm(sum, mulm(pref[i - 1], arr[i]));
  }
  cout << sum;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

using ll = long long;

int addm(int a, int b) { return ((ll)a + b) % MOD; }

int mulm(int a, int b) { return ((ll)a * b) % MOD; }

int main() {
  int n;
  vector<int> arr(n), pref(n);
  for (auto &x : arr)
    cin >> x;
  pref[0] = arr[0];
  for (int i = 1; i < n; i++) {
    pref[i] = addm(pref[i - 1], arr[i]);
  }
  int sum = 0;
  for (int i = 1; i < n; i++) {
    sum = addm(sum, mulm(pref[i - 1], arr[i]));
  }
  cout << sum;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> add(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = N - 1; i >= 0; i--) {
    add[i] = (i == N - 1 ? A[i] : add[i + 1] + A[i]);
    add[i] %= 1000000007;
  }
  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans += (A[i] * add[i + 1]) % 1000000007;
    ans %= 1000000007;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
const int maxn = 1e6 + 10;
#define p 1000000007
int main() {
  fast;
  ll n, s = 0, ans = 0;
  cin >> n;
  vector<ll> arr(n);
  for (auto &it : arr) {
    cin >> it;
    s += it;
  }
  for (auto i = n - 1; i >= 0; --i) {
    s -= arr[i];
    ans += (s % p * arr[i]) % p;
    ans %= p;
  }
  cout << ans << '\\n';
  return 0;
}
// prime func
// subset production
// factorization
// modpow
// modinv ( a^(p-2)=a^(-1) (mod p) )
// sieve
// ncr
// euler totien func (co prime divisiors upto n)
// matrix exponentation
// dsu
// Trees
// Graphs(dfs,bfs,shortes path etc.)
// DP-Recursion

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
const int maxn = 1e6 + 10;
#define p 1000000007
int main() {
  fast;
  ll n, t, s = 0;
  cin >> n;
  vector<ll> arr(n);
  for (auto &it : arr)
    cin >> it;
  for (auto i = 0; i < n; ++i) {
    if (arr[i] == 0)
      continue;
    for (auto j = i + 1; j < n; ++j) {
      s += (arr[i] * arr[j]) % p;
    }
  }
  cout << s % p << '\\n';
  return 0;
}
// prime func
// subset production
// factorization
// modpow
// modinv ( a^(p-2)=a^(-1) (mod p) )
// sieve
// ncr
// euler totien func (co prime divisiors upto n)
// matrix exponentation
// dsu
// Trees
// Graphs(dfs,bfs,shortes path etc.)
// DP-Recursion
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() {
  ll N;
  ll MOD = 1000000007;
  cin >> N;
  ll A[N];
  ll sum = 0;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    sum = (sum + A[i]) % MOD;
  }
  ll ans = 0;
  for (ll i = 0; i < N - 1; i++) {
    sum = (sum - A[i] + MOD) % MOD;
    ll a = (A[i] * sum) % MOD;
    ans = (ans + a) % MOD;
  }
  cout << ans << endl;
}


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() {
  ll N;
  cin >> N;
  ll A[N];
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  ll ans = 0;
  ll MOD = 1000000007;
  for (ll i = 0; i < N - 1; i++) {
    for (ll j = i + 1; j < N; j++) {
      ans += (A[i] * A[j]) % MOD;
      ans = ans % MOD;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h> //AtCoder
#include <iostream>      //Codeforces
#include <vector>        //Codeforces

#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Vc vector<char>
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } // 最大公約数
int lcm(int a, int b) { return a / gcd(a, b) * b; }     // 最小公倍数

const int MOD = 1000000007; // 10^9 + 7
struct mint {
  int x;
  mint(int x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
/****************************** START ******************************/

signed main() {
  int N;
  cin >> N;
  Vi A(N);
  int mod = 1000000007;
  rep(i, 0, N) { cin >> A.at(i); }

  mint sum = 0;
  rep(i, 0, N) { sum += A.at(i); }
  mint sqsum = 0;
  rep(i, 0, N) { sqsum += A.at(i) * A.at(i); }

  cout << (sum * sum - sqsum) / 2;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> //AtCoder
#include <iostream>      //Codeforces
#include <vector>        //Codeforces

#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Vc vector<char>
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } // 最大公約数
int lcm(int a, int b) { return a / gcd(a, b) * b; }     // 最小公倍数

/****************************** START ******************************/

signed main() {
  int N;
  cin >> N;
  Vi A(N);
  rep(i, 0, N) { cin >> A.at(i); }
  int mod = 1000000007;

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

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

const ll mod = 1000000000 + 7;

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


Error: TLE

Fixed submission:

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

const ll mod = 1000000000 + 7;

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 tmp = a[i] * a[j];
      tmp %= mod;
      ans += tmp;
      ans %= mod;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  long long A[N];

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

  long long count_sum = 0;
  long long count = 0;
  long long count_pre = 0;
  long long mod = pow(10, 9) + 7;

  for (long long i = 0; i < N; i++) {
    count_sum += A[i];
  }

  for (long long i = 0; i < N; i++) {
    count_sum -= A[i];
    count_pre = count_sum % mod;
    count += (count_pre * A[i]) % mod;
    count_pre = 0;
  }
  cout << count % mod << endl;
}

Error: TLE

Fixed submission:

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

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

  long long A[N];

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

  long long count = 0;
  long long count_pre = 0;
  long long mod = pow(10, 9) + 7;

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

#include <cmath>
#include <iostream>
#include <stdio.h>
using namespace std;
int mod1 = 1000000007;
bool is_integer(double x) { return floor(x) == ceil(x); }
int main() {
  int n;
  long long int a[200000];
  long long int b[200000] = {0};
  long long int sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = n - 2; i >= 0; i--) {
    b[i] = (b[i + 1] + a[i + 1]) % mod1;
  }
  for (int i = 0; i < n - 1; i++) {
    sum += (a[i] * b[i]) % mod1;
    sum %= mod1;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <stdio.h>
using namespace std;
bool is_integer(double x) { return floor(x) == ceil(x); }
int main() {
  int n;
  unsigned long int a[200000];
  unsigned int sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] = a[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum = (sum + (a[i] * a[j]) % 1000000007) % 1000000007;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long

// modeは余り
int main() {
  ll N;
  std::cin >> N;

  ll sum = 0;
  ll mod = 1000000007;

  std::vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    ll x;
    std::cin >> x;
    A[i] = x;
  }

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

  std::cout << sum << std::endl;
}

Error: TLE

Fixed submission:

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

#define ll long long

// modeは余り
int main() {
  ll N;
  std::cin >> N;

  ll sum = 0;
  ll mod = 1000000007;

  std::vector<ll> A(N);
  std::map<ll, ll> mp;
  for (ll i = 0; i < N; i++) {
    ll x;
    std::cin >> x;
    A[i] = x;
    mp[x]++;
  }

  std::sort(A.begin(), A.end());

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

//"BISMILLAHIR RAHMANIR RAHIM"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const long long M = 1e9 + 7;
int main() {
  ll n, i, j, ans = 0, s = 0;
  cin >> n;
  ll ara[n];
  for (i = 0; i < n; i++) {
    cin >> ara[i];
  }
  ll pre[n] = {0};
  pre[0] = ara[0];
  for (i = 1; i < n; i++) {
    pre[i] = (pre[i - 1] + ara[i]) % M;
  }
  for (i = 0; i < n; i++) {
    s = (ara[i] * (pre[n - 1] - pre[i])) % M;
    ans = (ans + s) % M;
  }
  ans = (ans + M) % M;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

//"BISMILLAHIR RAHMANIR RAHIM"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const long long M = 1e9 + 7;
int main() {
  ll n, i, j, ans = 0;
  cin >> n;
  vector<ll> ara(n);
  for (i = 0; i < n; i++)
    cin >> ara[i];
  for (i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      ans = (ans + ara[i] * ara[j]) % M;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7, N = 2e5 + 10;
ll q[N], pre[N];
int main() {
  int n;
  scanf("%d", &n);
  ll res = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &q[i]);
    pre[i] = q[i];
  }
  for (int i = 1; i <= n; i++) {
    pre[i] = (pre[i] + pre[i - 1]) % mod;
    res = (res + q[i] * pre[i - 1] % mod) % mod;
  }

  printf("%lld", res);
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7, N = 2e5 + 10;
ll q[N];
int main() {
  int n;
  scanf("%d", &n);
  ll res = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &q[i]);
    for (int j = 0; j < i; j++) {
      res = (res + q[i] * q[j] % mod) % mod;
    }
  }
  printf("%lld", res);
}