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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<int, int> P;

const long double EPS = 1e-10;
const long long INF = 1e18;
const int MOD = (int)1e9 + 7;
const long double PI = acos(-1.0L);
int main() { // TODO
  ll N;
  cin >> N;
  vector<ll> A(N);
  ll s = 0;

  ll ans = 0;
  rep1(N) {
    ll a;
    cin >> a;
    ll aa = a % MOD;
    ans = (ans % MOD) + ((aa % MOD) * s);
    s = (s % MOD) + (a % MOD);
  }

  cout << ans % MOD << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<int, int> P;

const long double EPS = 1e-10;
const long long INF = 1e18;
const int MOD = (int)1e9 + 7;
const long double PI = acos(-1.0L);

int main() { // TODO
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep1(N) {
    ll a;
    cin >> a;
    A[i] = a % MOD;
  }

  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      ans = (ans % MOD) + (A[i] * A[j]);
    }
  }
  cout << ans % MOD << endl;

  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;           //>10^18
typedef long double ld;         // 3.3621e-4932 ~ 1.18937e4932
typedef unsigned long long ull; //>10^20
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int n;
ll a[MAXN];
ll s[MAXN] = {0};
int main(int argc, char *argv[]) {
  scanf("%ull", &n);
  ll cnt = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    cnt += a[i];
  }
  for (int i = 1; i <= n; i++) {
    s[i] = cnt - a[i];
    cnt -= a[i];
  }
  int ans = 0, cot;
  for (int i = 1; i <= n; i++) {
    cot = ((a[i] % MOD) * (s[i] % MOD)) % MOD;
    ans = ((ans % MOD) + (cot % MOD)) % MOD;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
using namespace std;
typedef long long ll;           //>10^18
typedef long double ld;         // 3.3621e-4932 ~ 1.18937e4932
typedef unsigned long long ull; //>10^20
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
ull n;
ull ans = 0, cnt = 0;
ull a[MAXN];
ull s[MAXN] = {0};
int main(int argc, char *argv[]) {
  scanf("%ull", &n);
  for (ull i = 1; i <= n; i++) {
    scanf("%ull", &a[i]);
    cnt += a[i];
  }
  for (ull i = 1; i < n; i++) {
    s[i] = cnt - a[i];
    cnt -= a[i];
  }
  ull ans = 0, cot = 0;
  for (ull i = 1; i <= n; i++) {
    cot = ((a[i] % MOD) * (s[i] % MOD)) % MOD;
    ans = ((ans % MOD) + (cot % MOD)) % MOD;
  }
  printf("%ull", ans);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  ll A[N];
  rep(i, N) cin >> A[i];
  ll NUM = 1000000007;

  ll sum_A[N];
  rep(i, N) {
    int k = N - i - 1;
    if (k == N - 1)
      sum_A[k] = A[k] % NUM;
    else
      sum_A[k] = (sum_A[k + 1] + A[k]) % NUM;
  }

  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans += (A[i] * sum_A[i + 1]) % NUM;
  }

  cout << ans % NUM << 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;
  ll A[N];
  rep(i, N) cin >> A[i];

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

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

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

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

  int64_t sum = 0;
  int64_t X = 0;
  for (int i = 0; i < N; i++) {
    X = X + A[i];
    X = X % 1000000007;
  }

  for (int i = 0; i < N - 1; i++) {
    int64_t Y;
    if (X - A[i] % 1000000007 < 0) {
      X = X + 1000000007;
    }
    X = X - A[i] % 1000000007;
    Y = A[i] * X;
    Y = Y % 1000000007;
    sum = sum + Y;
    sum = sum % 1000000007;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 1e9 + 7;

ll n, a;
ll rpta, suma = 0;
// string ws="aji";

vector<ll> nros;
int main() {
  // your code goes here
  cin >> n;

  while (n) {
    cin >> a;
    nros.push_back(a);
    n--;
  }
  vector<ll> sumas_acum(nros.size() + 1);
  for (int i = 1; i < (int)sumas_acum.size(); i++) {
    sumas_acum[i] = sumas_acum[i - 1] + nros[i - 1];
    // cout << sumas_acum[i]<<" ";
  }

  for (int i = 0; i < (int)nros.size() - 1; i++) {
    suma = 0;
    /*for(int j=i+1; j<nros.size(); j++){

        suma = (suma%M   + nros[j]%M)%M;
    }*/
    suma = (sumas_acum[nros.size()] - sumas_acum[i + 1]) % M;
    suma = (suma * nros[i] % M) % M;
    rpta = (rpta % M + suma) % M;
  }

  cout << rpta << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 1e9 + 7;

ll n, a;
ll rpta, suma = 0;
// string ws="aji";
vector<ll> nros;
int main() {
  // your code goes here
  cin >> n;

  while (n) {
    cin >> a;
    nros.push_back(a);
    n--;
  }

  for (int i = 0; i < nros.size() - 1; i++) {
    suma = 0;
    for (int j = i + 1; j < nros.size(); j++) {

      suma = (suma % M + nros[j] % M) % M;
    }
    suma = (suma % M * nros[i] % M) % M;
    rpta = (rpta % M + suma % M) % M;
  }

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

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpi;

#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define tcase()                                                                \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

const int MOD = 1e9 + 7; // 998244353;
const int MX = 2e5 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);

void setIO(string name = "") {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (sz(name)) {
    freopen((name + ".in").c_str(), "r", stdin);
    freopen((name + ".out").c_str(), "w", stdout);
  }
}

int main() {

  setIO();

  int n;
  cin >> n;

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

  ll total = 0, p = 0;

  for (int i = 0; i < n; i++) {
    total += (arr[i] % MOD) * (p % MOD);
    total %= MOD;
    p += arr[i];
  }

  cout << total;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpi;

#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define tcase()                                                                \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

const int MOD = 1e9 + 7; // 998244353;
const int MX = 2e5 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);

void setIO(string name = "") {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (sz(name)) {
    freopen((name + ".in").c_str(), "r", stdin);
    freopen((name + ".out").c_str(), "w", stdout);
  }
}

int main() {

  setIO();

  int n;
  cin >> n;

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

  ll ans = 0;

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += (arr[i] % MOD) * (arr[j] % MOD);
      ans %= MOD;
    }
  }

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

#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;

using lll = long long;
using ull = unsigned long long;
using namespace std;
const ull MOD = 1000000007;

int main() {
  lll ii, jj, kk;
  vector<int> ret;
  int n;
  vector<lll> a;

  cin >> n;
  a.resize(n);

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

  lll sumAdd = 0;

  for (ii = 0; ii < a.size(); ii++) {
    sumAdd += a[ii];
    sumAdd %= MOD;
  }
  lll sum = 0;

  for (ii = 0; ii < a.size(); ii++) {
    // if(ii % 1000 == 0){
    //   cout << ii << endl;
    // }
    // for(jj=ii+1;jj<a.size();jj++){
    //   sum = a[ii] * a[jj]   + sum;
    //   sum %= MOD;
    // }

    sumAdd -= a[ii];
    if (sumAdd < 0)
      sumAdd += MOD;

    sum += a[ii] * sumAdd;
    sum %= MOD;
  }
  // cout << MOD << endl;
  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
const int INF = 0x7fffffff;

using lll = long long;
using ull = unsigned long long;
using namespace std;
const lll MOD = 1000000007;

int main() {
  lll ii, jj, kk;
  vector<int> ret;
  int n;
  vector<lll> a;

  cin >> n;
  a.resize(n);
  for (ii = 0; ii < n; ii++) {
    cin >> a[ii];
  }
  lll sum = 0;

  for (ii = 0; ii < a.size() - 1; ii++) {

    for (jj = ii + 1; jj < a.size(); jj++) {
      sum = a[ii] * a[jj] + sum;
      sum %= MOD;
    }
  }
  // cout << MOD << endl;
  cout << sum << endl;

  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

int main() {

  int n;
  cin >> n;

  vector<ll> a(n);
  vector<ll> b(1);

  rep(i, n) {
    cin >> a[i];
    b.push_back((b[i] + a[i]) % MODN);
  }

  ll ans = 0;

  rep(i, n) {
    ll sum = b[n] - b[i + 1];
    if (sum < 0)
      sum += MODN;

    sum = sum % MODN;
    ans = (ans + (a[i] * sum) % MODN) % MODN;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

int main() {

  int n;
  cin >> n;

  vector<ll> a(n);
  vector<ll> b(1);

  rep(i, n) {
    cin >> a[i];
    b.push_back((b[i] + a[i]) % MODN);
  }

  ll ans = 0;

  rep(i, n) {
    ll sum = b[n] - b[i + 1];
    assert(sum >= 0);

    sum = sum % MODN;
    ans = (ans + (a[i] * sum) % MODN) % MODN;
  }

  cout << ans << endl;

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

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

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


Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define db double
#define pb push_back
#define wrln cout << '\\n';
#define pii pair<int, int>

using namespace std;
const int H = 1e9 + 7;
const int N = 2e5 + 100;

unsigned long long res, f[N], a[N], n;

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

  // freopen("test11.INP","r",stdin);
  // freopen("test.OUT","w",stdout);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[i] = a[i] + f[i - 1];
  }
  for (int i = n; i > 0; i--) {
    res += ((a[i] % H) * (f[i - 1] % H)) % H;
    res %= H;
  }

  cout << res;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define db double
#define pb push_back
#define wrln cout << '\\n';
#define pii pair<int, int>

using namespace std;
const int H = 1e9 + 7;
const int N = 1e5 + 100;

long long res, f[N], a[N], n;

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

  // freopen("test11.INP","r",stdin);
  // freopen("test.OUT","w",stdout);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[i] = a[i] + f[i - 1];
    f[i] %= H;
  }
  for (int i = 1; i <= n; i++) {
    res += ((0LL + f[n] - f[i]) % H * a[i]) % H;
    res %= H;
  }

  cout << res;
}
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;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  vector<ll> B(N);
  B[N - 1] = A[N - 1];
  for (int i = N - 2; i >= 0; i--)
    B[i] = (B[i + 1] + A[i]) % 1000000007;

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

  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;

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] % 1000000007;
    }
    ans %= 1000000007;
  }

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

#include <iostream>
using namespace std;

const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  int list[n];
  long long int sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> list[i];
    sum += list[i];
    sum %= mod;
  }
  long long int result = 0;
  for (int i = 0; i < n; ++i) {
    sum -= list[i];
    if (sum < 0)
      sum += mod;
    result += (sum * list[i]);
    result %= mod;
  }
  cout << result << "\\n";
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  long long int p[N];
  for (int i = 0; i < N; ++i)
    cin >> p[i];
  long long int result = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      result += p[i] * p[j];
      result = result % (1000000007);
    }
  }
  cout << result << "\\n";
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;
#define MOD 1000000000 + 7
int main() {
  int N;
  cin >> N;
  long long A[N];
  long long sum(0);
  long long sum_A(0);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    sum += (A[i] * sum_A) % (MOD);
    sum %= (MOD);
    sum_A += A[i];
    sum_A %= (MOD);
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <variant>
#include <vector>

using namespace std;

int main(void) {
  int n = 0;
  cin >> n;
  int sum = 0;
  long long int tot = 0;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum = (sum + a[i]) % 1000000007;
  }
  for (int i = 0; i < n; i++) {
    sum = (1000000007 + sum - a[i]) % 1000000007;
    tot = (tot + a[i] * sum) % 1000000007;
  }
  cout << tot;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <variant>
#include <vector>

using namespace std;

int main(void) {
  int n = 0;
  cin >> n;
  long long int tot = 0;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int k = 0; k < i; k++) {
      tot = (tot + a[i] * a[k]) % 1000000007;
    }
  }
  cout << tot;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  ll x = 0, ans = 0;
  int mod = 1000000007;
  rep(i, n) {
    ans += (a[i] * x) % mod;
    ans %= mod;
    x += a[i];
    x %= mod;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

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

const int mod = 1e9 + 7;
const int max_n = 2e5 + 10;
long long a[max_n];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
    sum = sum % mod;
  }
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    sum -= a[i];
    if (sum < 0)
      sum += mod;
    ans += a[i] * sum;
    ans = ans % mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

const int mod = 1e9 + 7;
const int max_n = 2e5 + 10;
long long a[max_n];

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

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

void execute() {
  int N;
  ll A[200000];

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

  ll s = 0;
  ll v = 0;
  for (int i = N - 1; 0 < i; i--) {
    v += A[i];
    v %= (1000000007);
    s += A[i - 1] * v;
    s %= (1000000007);
  }

  cout << s << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

void execute() {
  int N;
  ll A[200000];

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

  ll s = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      s += A[i] * A[j];
      s %= (1000000007);
    }
  }

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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
  ll n, s = 0, ans = 0;
  ll MOD = 1e9 + 7;
  scanf("%lld", &n);
  ll a[n];
  for (auto &k : a) {
    scanf("%lld", &k);
    s += k;
  }
  for (int i = 0; i < n - 1; i++) {
    s -= a[i];
    ans += ((s) % MOD * (a[i]) % MOD) % MOD;
    ans %= MOD;
  }
  ans %= MOD;
  printf("%lld", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
  ll n, s = 0;
  ll MOD = 1e9 + 7;
  scanf("%lld", &n);
  ll a[n];
  for (auto &k : a) {
    scanf("%lld", &k);
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      s += (a[i] * a[j]) % MOD;
      s %= MOD;
    }
  }
  printf("%lld", s);
  return 0;
}