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

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define mod 1000000007
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
using graph = vector<vector<int>>;
using Grid = vector<vector<bool>>;

int main() {
  // cin高速化
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, ans, sum;
  cin >> n;
  vll a(n, 0);
  ans = 0;
  sum = 0;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
    sum %= mod;
  }

  REP(i, n) {
    sum -= a[i];
    if (sum < 0)
      sum += mod;

    ans += a[i] * sum;
    ans %= mod;
  }

  cout << ans << "\\n";
  return 0;
}

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


Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define mod 1000000007
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
using graph = vector<vector<int>>;
using Grid = vector<vector<bool>>;

int main() {
  // cin高速化
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, ans, sum;
  vll a(n, 0);
  cin >> n;
  ans = 0;
  sum = 0;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
    sum %= mod;
  }

  REP(i, n) {
    sum -= a[i];
    if (sum < 0)
      sum += mod;

    ans += a[i] * sum;
    ans %= mod;
  }

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

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

const long long MOD = 1e9 + 7;

const int maxn = 1e6;

long long mod(long long value) {
  if (value >= 0) {
    return value % MOD;
  }
  for (int i = 1;; i++) {
    long long tmp = value + i * MOD;
    if (tmp >= 0) {
      return tmp;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> a(n);
  vector<long long> sum(maxn, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long tmp = 0;
  for (int i = n - 1; i >= 1; i--) {
    tmp += a[i];
    sum[i] = tmp;
    if (sum[i] >= MOD) {
      sum[i] = mod(sum[i]);
    }
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans += a[i] * sum[i + 1];
    if (ans >= MOD) {
      ans = mod(ans);
    }
  }
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

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

const long long MOD = 1e9 + 7;

long long mod(long long value) { return value % MOD; }

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      long long tmp = a[i] * a[j];
      if (tmp >= 1e9 + 7) {
        ans += mod(tmp);
      } else {
        ans += tmp;
      }
      if (ans >= 1e9 + 7) {
        ans = mod(ans);
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int i, j;
  long long mod = 1000000007;
  int n;
  cin >> n;
  vector<long long> a(n, 0), b(n + 1, 0);
  for (i = 0; i < n; i++) {
    cin >> a.at(i);
    b.at(i + 1) += b.at(i) + a.at(i);
  }

  long long ans = 0;
  for (i = 0; i < n; i++) {
    long long sum = ((b.at(n) - b.at(i + 1)) % mod);
    ans += a.at(i) * sum;
    ans %= mod;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int i, j;
  int mod = 1000000007;
  int n;
  cin >> n;
  vector<long long> a(n, 0);
  for (i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  long long ans = 0;
  for (i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      ans += a.at(i) * a.at(j);
      ans %= mod;
    }
    ans %= mod;
  }
  ans %= mod;
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <cstdint>
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1e9 + 7;

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

  vector<int> A(n);
  vector<long long> S(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    S[i + 1] = S[i] + A[i];
  }

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    long long sum = (S[n] - S[i + 1]) % MOD;
    ans += A[i] * sum;
    ans %= MOD;
    // cout << "---- i == " << i << " ----\\n";
    // cout << "A[i] == " << A[i] << "\\n";
    // cout << "S[n] - S[i+1] == " << S[n] - S[i+1] << "\\n";
    // cout << "to be added:" << A[i] * (S[n] - S[i]) << "\\n";
    // cout << "ans:" << ans << "\\n";
  }

  cout << ans << "\\n";
}

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 <cstdint>
#include <iostream>
#include <vector>
#define MOD 1000000007
using namespace std;

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

  vector<uint64_t> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  vector<uint64_t> S(n);
  S[0] = 0;
  for (int i = 0; i < n; i++) {
    S[i + 1] = S[i] + A[i];
  }

  uint64_t ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans += A[i] * ((S[n] - S[i + 1]) % MOD);
    ans %= MOD;
    // cout << "---- i == " << i << " ----\\n";
    // cout << "A[i] == " << A[i] << "\\n";
    // cout << "S[n] - S[i+1] == " << S[n] - S[i+1] << "\\n";
    // cout << "to be added:" << A[i] * (S[n] - S[i]) << "\\n";
    // cout << "ans:" << ans << "\\n";
  }

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

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

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long ans = 0;
  long long x = 1000000000 + 7;
  vector<long long> partial;

  rep(i, n) {
    cin >> a.at(i);
    // a.at(i) = a.at(i);
  }

  int partial_s = 0;
  for (int i = 0; i < n; i++) {
    partial_s += a.at(n - 1 - i);
    partial_s = partial_s % x;
    partial.push_back(partial_s);
  }

  reverse(partial.begin(), partial.end());

  // rep(i, n){
  //     cout << partial.at(i) << endl;
  // }

  for (int i = 0; i < n - 1; i++) {
    ans += a.at(i) * partial.at(i + 1) % x;
    ans = ans % x;
    // cout << a.at(i) << " " << partial.at(i+1) << endl;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long ans = 0;
  long long x = 1000000000 + 7;

  rep(i, n) {
    cin >> a.at(i);
    // a.at(i) = a.at(i);
  }

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += a.at(i) * a.at(j) % (x);
      ans = ans % x;
      // cout << a.at(i)  << " " << a.at(j) << " " << ans << endl;
    }
  }

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

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

Error: TLE

Fixed submission:

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

#include "bits/stdc++.h"
#include <set>

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#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 int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}

template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

long long modsub(long long val, long long m) {
  long long res = val % m;
  if (res < 0)
    res += m;
  return res;
}

signed main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  int sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    sum %= mod;
  }
  int ret = 0;
  rep(i, n - 1) {
    sum = modsub((sum % mod) - (a[i] % mod), mod);
    int sub = sum * a[i] % mod;
    //	print(sub);
    ret += sub;
    ret %= mod;
  }
  print(ret);
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <set>

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#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 int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}

template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

signed main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int ret = 0;
  rep(i, n - 1) {
    FOR(j, i + 1, n) {
      int aij = a[i] * a[j] % mod;
      ret += aij;
      ret %= mod;
    }
  }
  print(ret);
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;

ll power(ll x, ll y) {
  x %= MOD;
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = (ans * x) % MOD;
    y >>= 1LL;
    x = (x * x) % MOD;
  }
  return ans;
}

ll ad(ll a, ll b) { return ((a % MOD + b % MOD) % MOD); }
ll sub(ll a, ll b) { return ((a % MOD - b % MOD + MOD) % MOD); }
ll mul(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); }
ll divi(ll a, ll b) { return (mul(a, power(b, MOD - 2)) % MOD); }

int findProductSum(ll A[], ll n) {
  ll array_sum = 0;
  for (int i = 0; i < n; i++)
    array_sum = ad(array_sum, A[i]);
  ll array_sum_square = mul(array_sum, array_sum);
  ll individual_square_sum = 0;
  for (int i = 0; i < n; i++)
    individual_square_sum = ad(individual_square_sum, mul(A[i], A[i]));
  return divi(sub(array_sum_square, individual_square_sum), 2);
}

int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  cout << findProductSum(a, n);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ll long long int
#define ld long double
using namespace std;
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;

ll ad(ll a, ll b) { return ((a % MOD + b % MOD) % MOD); }
ll sub(ll a, ll b) { return ((a % MOD - b % MOD + MOD) % MOD); }
ll mul(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); }

int main() {
  fast;
  ll n;
  cin >> n;
  ll a[n], sum = 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) {
      ll ans = mul(a[i], a[j]);
      sum = ad(sum, ans);
    }
  }
  cout << sum;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

  uint64_t sig = 0;
  for (int i = 0; i < n; i++) {
    sig += a[i];
  }

  vector<uint64_t> sig_(n);
  for (int i = 0; i < n; i++) {
    sig = sig - a[i];
    sig_[i] = sig % div;
    // cout << sig_[i] << endl;
  }

  uint64_t sum = 0;
  for (int i = 0; i < n - 1; i++) {
    uint64_t p = a[i] * sig_[i];

    sum += p % div;
    sum = sum % div;
  }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

  uint64_t sum = 0;
  uint64_t div = 1000000000 + 7;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      uint64_t p = a[i] * a[j];
      // sum += p;

      sum += p % div;
    }
    sum = sum % div;
  }

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

#include <iostream>
#include <vector>

const long long int MOD = 1e9 + 7;

int main() {
  int a;

  std::cin >> a;

  std::vector<long long int> vec(a);

  long long int sum = 0;

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

  long long int c = 0;

  for (int i = 0; i < a; i++) {
    sum -= vec[i];
    c = ((c % MOD) + ((vec[i] % MOD) * (sum % MOD)) % MOD) % MOD;
  }

  std::cout << c << std::endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

const long long int MOD = 1e9 + 7;

int main() {
  int a;

  std::cin >> a;

  std::vector<long long int> vec(a);

  for (int i = 0; i < a; i++) {
    std::cin >> vec[i];
  }

  long long int c = 0;

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

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

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

int main() {
  long long int n;
  cin >> n;
  long long int mod = 1000000007;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  long long int x = 0, ans = 0;
  for (int j = 1; j < n; j++) {
    x += a.at(j - 1);
    x %= mod;
    ans += a.at(j) * x;
    ans %= mod;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long int n;
  cin >> n;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  long long int ans = 0;
  for (int j = 0; j < n - 1; j++) {
    for (int k = j + 1; k < n; k++) {
      ans += a.at(j) * a.at(k) % 1000000007;
      ans %= 1000000007;
    }
  }
  cout << ans % 1000000007 << 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 dekai = 1000000007, ans = 0, sum = 0;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (long long i = 0; i < n; i++) {
    ans += (sum * a.at(i) % dekai);
    ans %= dekai;
    sum += (a.at(i) % dekai);
    sum %= dekai;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long n;
  cin >> n;
  long long dekai = 1000000007, ans = 0;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  for (long long i = 0; i < n - 1; i++) {
    for (long long j = i + 1; j < n; j++) {
      ans += a.at(i) * a.at(j);
      ans %= dekai;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
#define rep(i, N) for (int i = 0; i < N; i++)
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long int> A(N);
  vector<long int> B(N - 1);
  rep(i, N) { cin >> A.at(i); }
  long int ans = 0;
  B.at(0) = A.at(N - 1);
  for (int i = 1; i < N - 1; i++) {
    B.at(i) = B.at(i - 1) + A.at(N - 1 - i);
    B.at(i) = B.at(i) % 1000000007;
  }
  rep(i, N - 1) { ans += A.at(i) * B.at(N - 2 - i) % 1000000007; }
  cout << ans % 1000000007 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
#define rep(i, N) for (int i = 0; i < N; i++)
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long int> A(N);
  rep(i, N) { cin >> A.at(i); }
  long int ans = 0;
  rep(i, N - 1) {
    for (int j = i + 1; j < N; j++) {
      ans += A.at(i) * A.at(j) % 1000000007;
    }
  }
  cout << ans % 1000000007 << endl;

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

#include <iostream>
using namespace std;
#define mod 1000000007
int main() {
  int n;
  cin >> n;
  long long int arr[n + 1], sum[n + 1], prod = 1, ans = 0;
  sum[n] = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i] = arr[i] % mod;
  }
  for (int i = n - 1; i > 0; i--) {
    sum[i] = arr[i] + sum[i + 1];
    sum[i] %= 1000000007;
  }
  // cout<<total<<endl;
  for (int i = 0; i < n - 1; i++) {
    prod = (arr[i] * sum[i + 1]) % mod;
    prod = prod % mod;
    ans = ans + prod;
    ans = ans % mod;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define mod 1000000007
int main() {
  int n;
  cin >> n;
  long long int arr[n + 1], prod = 1, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i] = arr[i] % mod;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      prod = (arr[i] * arr[j]) % mod;
      prod = prod % mod;
      ans = ans + prod;
      ans = ans % mod;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  unsigned long long A[N];
  unsigned long long s[N + 1];
  unsigned long long sum = 0;
  s[0] = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    s[i + 1] = s[i] + A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    sum = sum + A[i] * ((s[N] - s[i + 1]) % 1000000007);
    sum = sum % 1000000007;
  }
  printf("%llu\\n", sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  unsigned long long A[N];
  unsigned long long sum = 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++) {
      sum = (sum % 1000000007) + (A[i] % 1000000007) * (A[j] % 1000000007);
    }
  }

  printf("%llu\\n", sum % 1000000007);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int n;
  cin >> n;
  vector<long long> a(n);
  long long sum = 0;
  const int MOD = 1000000007;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    sum += a.at(i);
    sum %= MOD;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    sum -= a.at(i);
    if (sum < 0) {
      sum += MOD;
    }
    ans += a.at(i) % MOD * sum % MOD;
  }
  cout << ans % MOD << endl;
}


Error: TLE

Fixed submission:

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

int main() {

  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  long long ans = 0;
  const int MOD = 1000000007;
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < j; i++) {
      ans += a.at(i) % MOD * a.at(j) % MOD;
      ans %= MOD;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>

#define ll long long int
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (int i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)

using namespace std;

int mod = (int)1e9 + 7;

long long mMod(long long a, long long b) {
  long long res = 0; // Initialize result

  // Update a if it is more than
  // or equal to mod
  a %= mod;

  while (b) {
    // If b is odd, add a with result
    if (b & 1)
      res = (res + a) % mod;

    // Here we assume that doing 2*a
    // doesn't cause overflow
    a = (2 * a) % mod;

    b >>= 1; // b = b / 2
  }

  return res;
}

void util() {

  int n;
  cin >> n;

  long long a[n];

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

  long long ans = 0;

  long long sum = 0;

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

  for (int i = 0; i < n - 1; i++) {

    sum -= a[i];

    ans = (ans + mMod(a[i], sum)) % mod;
  }

  cout << ans << endl;
}

int main() {
  /* code */

  util();

  return 0;
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>

#define ll long long int
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define rep(n) for (int i = 0; i < n; ++i)
#define REP(i, k, n) for (int i = k; i <= n; ++i)
#define REPR(i, k, n) for (int i = k; i >= n; --i)

using namespace std;

int mod = (int)1e9 + 7;

void util() {

  int n;
  cin >> n;

  long long a[n];

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

  long long ans = 0;

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {

      ans = (ans + (a[i] * a[j]) % mod) % mod;
    }
  }

  cout << ans << endl;
}

int main() {
  /* code */

  util();

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

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

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

  int n;
  cin >> n;
  vector<long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<long> c_sum(n);
  c_sum[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; --i) {
    c_sum[i] = c_sum[i + 1] + a[i];
    c_sum[i] %= 1000000007;
  }

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

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  unsigned 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;
    }
  }

  cout << ans << endl;

  return 0;
}