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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define prtarr(n, len)                                                         \\
  {                                                                            \\
    for (int i = 0; i < len; i++)                                              \\
      cout << n[i] << " ";                                                     \\
  }

using namespace std;

typedef long long int ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;
using namespace std;

int n;
vll lists;
int main() {
  // freopen("input.txt", "r", stdin) ;
  // freopen("output.txt", "w", stdout) ;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  vll dp(n, 0);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    ll sub;
    cin >> sub;
    sum += sub;
    lists.pb(sub);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == 0)
      dp[i] = sum;
    else
      dp[i] = (dp[i - 1] - lists[i - 1]);
  }
  for (int i = 0; i < n - 1; i++) {
    ans += ((dp[i + 1] % MOD) * lists[i]) % MOD;
  }
  cout << ans % MOD;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ll long long
#define ld long double
#define all(arr) arr.begin(), arr.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define INF 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846264338327950L
#define N 100005
#define mid(l, r) l + (r - l) / 2
#define vec vector<int>
#define vecl vector<ll>
#define umap unordered_map<ll, ll>
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define endl "\\n"
using namespace std;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
ll gcd(ll a, ll b) {
  if (!a)
    return b;
  return gcd(b % a, a);
}

ll findProductSum(vecl A, int n) {
  ll product = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      product = product + (A[i] * A[j]) % mod;
  return product % mod;
}

void test_case() {

  int n;
  cin >> n;

  vecl arr(n);
  rep(i, n) { cin >> arr[i]; }

  ll ans = 0;

  sort(all(arr));

  ans = findProductSum(arr, n);
  cout << (ans + mod) % mod << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int t = 1;
  // cin >> t;
  while (t--) {
    test_case();
  }
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define pb push_back
#define PQ priority_queue
#define make_unique(x)                                                         \\
  sort((x).begin(), (x).end());                                                \\
  (x).erase(unique((x).begin(), (x).end()), (x).end())

typedef long long ll;
typedef long double ld;

//__builtin_popcount(x)

using namespace std;

const int mod = 1e9 + 7;

ll add(ll a, ll b) {
  ll res = a + b;
  if (res >= mod)
    res -= mod;
  return res;
}

ll sub(ll a, ll b) {
  ll res = a - b + mod;
  if (res >= mod)
    res -= mod;
  return res;
}

ll mul(ll a, ll b) { return (((a % mod) * (b % mod)) % mod); }

/*vector<int> find_dividers(int x) {
    vector<int> dividers;

    for (int i = 1; i <= sqrt(x); i++) {
        if (x % i == 0) {
           dividers.push_back(i);
            if (i * i != x) {
                dividers.push_back(x / i);
            }
        }
    }

    return dividers;
}*/

/*int nok(int a, int b) {
    return a / __gcd(a, b) * b;
}*/

void solve() {
  int n;
  cin >> n;
  ll pref[n + 1];
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    pref[i] = a[i];
    if (i) {
      pref[i] = 1ll * a[i] + pref[i - 1];
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = add(ans, mul(sub(pref[n - 1], pref[i]), a[i]));
  }

  cout << ans << endl;
}

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

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define PQ priority_queue
#define make_unique(x)                                                         \\
  sort((x).begin(), (x).end());                                                \\
  (x).erase(unique((x).begin(), (x).end()), (x).end())

typedef long long ll;
typedef long double ld;

//__builtin_popcount(x)

using namespace std;

const int mod = 1e9 + 7;

ll add(ll a, ll b) {
  ll res = a + b;
  if (res >= mod)
    res -= mod;
  return res;
}

ll sub(ll a, ll b) {
  ll res = a - b + mod;
  if (res >= mod)
    res -= mod;
  return res;
}

ll mul(ll a, ll b) { return (((a % mod) * (b % mod)) % mod); }

/*vector<int> find_dividers(int x) {
    vector<int> dividers;

    for (int i = 1; i <= sqrt(x); i++) {
        if (x % i == 0) {
           dividers.push_back(i);
            if (i * i != x) {
                dividers.push_back(x / i);
            }
        }
    }

    return dividers;
}*/

/*int nok(int a, int b) {
    return a / __gcd(a, b) * b;
}*/

void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = add(mul(a[i], a[j]), ans);
    }
  }

  cout << ans << endl;
}

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

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

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

#include <iostream>

using namespace std;
typedef long long ll;
ll mod = 1000000007;

int main(void) {
  int N;
  ll A[200005], t = 0, t2 = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    t += A[i];
    t %= mod;
    t2 = t2 + ((A[i] % mod) * (A[i] % mod)) % mod;
    t2 %= mod;
  }
  ll ans = (((t * t) % mod) + (mod - t2)) % mod;
  if (ans < 0) {
    ans += mod;
  }
  ans *= (mod + 1) / 2;
  cout << ans % mod << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
typedef long long ll;
ll mod = 1000000007;

int main(void) {
  int N;
  ll A[200005];

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define WARI 1000000007

int main(void) {
  ll N;
  unsigned long long ans = 0, sum = 0, now = 0;
  cin >> N;
  vector<unsigned long long> A(N + 10);
  REP(i, N) { cin >> A[i]; }
  vector<unsigned long long> Now(N + 10);
  Now[0] = 0;
  FOR(i, 1, N) Now[i] = Now[i - 1] + A[i - 1];
  FOR(i, 0, N) {
    ans += (Now[i] % WARI) * A[i];
    ans %= WARI;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define WARI 1000000007

int main(void) {
  ll N;
  unsigned long long ans = 0;
  cin >> N;
  vector<unsigned long long> A(N + 10);
  REP(i, N) cin >> A[i];
  FOR(i, 0, N - 1) {
    FOR(j, i + 1, N) {
      ans += (A[i] * A[j]);
      if (ans >= WARI)
        ans %= WARI;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long

template <typename T> void drop(const T &x) {
  cout << x << '\\n';
  exit(0);
}

void solve() {
  ll mod = 1e9 + 7;
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> s(n + 1);
  for (int i = 0; i < n; ++i) {
    cin >> a.at(i);
    s.at(i + 1) = s.at(i) + a.at(i);
  }

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += ((__int128_t)(a.at(i)) * (__int128_t)(s.at(n) - s.at(i + 1))) % mod;
  }
  cout << (ans + mod) % mod << '\\n';
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();

  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long

template <typename T> void drop(const T &x) {
  cout << x << '\\n';
  exit(0);
}

void solve() {
  ll mod = 1e9 + 7;
  int n;
  cin >> n;
  vector<ll> a(n);
  ll ans = 0;
  for (int i = 0; i < n; ++i)
    cin >> a.at(i);

  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      ans += (a.at(i) * a.at(j)) % mod;
    }
  }
  cout << ans % mod << '\\n';
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
const double PI = acos(-1);

class Mint {
private:
  ll x;

public:
  Mint(ll x = 0) : x((x % mod + mod) % mod) {}
  ll Get() const { return x; }
  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(ll t) const {
    if (!t)
      return 1;
    Mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  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; }
};
inline ostream &operator<<(ostream &os, const Mint &a) { return os << a.Get(); }

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<Mint> s(n);
  s[0] = a[0];
  for (int i = 1; i < n; i++) {
    s[i] = s[i - 1] + a[i];
  }

  Mint ans = Mint();
  for (ll i = 0; i < n - 1; i++) {
    ans += Mint(a[i]) * (s[n - 1] - s[i]);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
const double PI = acos(-1);

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

  ll ans = 0;
  for (ll i = 0; i < n - 1; i++) {
    for (ll j = i + 1; j < n; j++) {
      ans += (a[i] % mod) * (a[j] % mod);
      ans %= mod;
    }
  }
  cout << ans << endl;
}
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 < (int)(n); i++)
using ll = unsigned long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n, 0);
  rep(i, n) { cin >> a.at(i); }
  ll sum = 0;
  vector<ll> b(n - 1, 0);
  rep(i, n - 1) {
    if (i == 0) {
      b.at(n - 2 - i) = a.at(n - 1 - i);
    } else {
      b.at(n - 2 - i) = b.at(n - 1 - i) + a.at(n - 1 - i);
    }
    b.at(n - 2 - i) %= 1000000007;
  }

  rep(i, n - 1) {
    sum += a.at(i) * b.at(i);
    sum %= 1000000007;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0), cout.tie(0);
#define ll long long
#define endl '\\n'
using namespace std;

int main() {

  ll n, M = 1e9 + 7;
  cin >> n;
  ll ar[n];

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

  ll sm = 0, ans = 0;

  for (int i = n - 1, j = n - 2; j >= 0; i--, j--) {
    sm = (sm % M + (ar[i] % M));

    ans = ((ans % M) + (ar[j] * sm) % M) % M;

    // cout<<sm<<endl;
    // cout<<ans<<endl;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0), cout.tie(0);
#define ll long long
#define endl '\\n'
using namespace std;

int main() {

  ll n, M = 1e9 + 7;
  cin >> n;
  ll ar[n];

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

  ll sm = 0;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sm = (sm % M + ((ar[i] * ar[j]) % M)) % M;
    }
  }

  cout << sm << endl;

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

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

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

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#include <string>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  ll sum = 0;
  ll answer = 0;
  rep(i, N) {
    cin >> A[i];
    sum += A[i];
    sum %= MOD;
  }
  for (int i = 0; i < N; i++) {
    sum -= A[i];
    if (sum < 0)
      sum += MOD;
    answer += A[i] * sum;
    answer %= MOD;
  }
  // if(D/S>T)cout<<"No"<<endl;
  cout << answer << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#include <string>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  ll answer = 0;
  rep(i, N) { cin >> A[i]; }

  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      answer += A[i] * A[j] % MOD;
      answer = answer % MOD;
      // cout<<A[i]*A[t]%MOD<<endl;
    }
  }
  // if(D/S>T)cout<<"No"<<endl;
  cout << answer << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  const unsigned int M = 1000000007;
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll sum = 0;
  ll ans = 0;
  for (ll i = 0; i < n - 1; i++) {
    sum = (sum % M + a[i] % M) % M;
    ans = (ans % M + (sum * (a[i + 1]) % M) % M) % M;
  }
  cout << ans % M;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  const unsigned int M = 1000000007;
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ssum = 0;
  for (ll i = 0; i < n; i++) {
    ll insum = 0;
    for (ll j = i + 1; j < n; j++) {
      insum = (insum % M + a[j] % M) % M;
    }
    ssum = (ssum % M + (a[i] % M * insum % M) % M) % M;
  }
  cout << ssum;

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

#include <algorithm>
#include <array>
#include <climits>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define IN(a, b) (a.find(b) != a.end())
#define p(a, b) make_pair(a, b)
#define readVec(a)                                                             \\
  for (int64_t __i = 0; __i < (int64_t)a.size(); __i++) {                      \\
    cin >> a[__i];                                                             \\
  }

// jimjam

template <typename T> void pMin(T &a, T b) {
  if (b < a) {
    a = b;
  }
}
template <typename T> void pMax(T &a, T b) {
  if (b > a) {
    a = b;
  }
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &c);
template <typename A, typename B>
std::ostream &operator<<(std::ostream &os, const std::pair<A, B> &c) {
  std::cout << "(" << c.first << ", " << c.second << ")";
  return os;
}

using namespace std;

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

  const int64_t mod = 1e9 + 7;
  int64_t n;
  cin >> n;
  vector<int64_t> a(n);
  readVec(a);

  int64_t ans = 0;

  int64_t sumBefore = a[0];
  for (int i = 1; i < n; i++) {
    ans += a[i] * sumBefore;
    ans %= mod;
    sumBefore %= mod;
    sumBefore += a[i];
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define IN(a, b) (a.find(b) != a.end())
#define p(a, b) make_pair(a, b)
#define readVec(a)                                                             \\
  for (int64_t __i = 0; __i < (int64_t)a.size(); __i++) {                      \\
    cin >> a[__i];                                                             \\
  }

// jimjam

template <typename T> void pMin(T &a, T b) {
  if (b < a) {
    a = b;
  }
}
template <typename T> void pMax(T &a, T b) {
  if (b > a) {
    a = b;
  }
}
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &c);
template <typename A, typename B>
std::ostream &operator<<(std::ostream &os, const std::pair<A, B> &c) {
  std::cout << "(" << c.first << ", " << c.second << ")";
  return os;
}

using namespace std;

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

  const int64_t mod = 1e9 + 7;
  int64_t n;
  cin >> n;
  vector<int64_t> a(n);
  readVec(a);

  int64_t sum = accumulate(a.begin(), a.end(), int64_t(0));
  sum %= mod;
  int64_t ans = 0;

  for (int64_t i : a) {
    ans += (sum - i) * i;
    ans %= mod;
  }
  assert(ans % 2 == 0);
  cout << ans / 2 << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long n;
  long long sum, sa;
  sum = 0;
  sa = 0;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    sa += a.at(i);
  }
  for (int i = 0; i < n; i++) {
    sa -= a.at(i);
    long long si = sa;
    si %= 1000000007;
    sum += a.at(i) * si;
    sum %= 1000000007;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1000000007;
ll findProductSum(int A[], int n) {
  vector<long long> dp(n);
  for (int i = n - 2; i >= 0; i--)
    dp[i] = (dp[i + 1] + A[i + 1]) % mod;
  ll individual_square_sum = 0;
  for (int i = 0; i < n - 1; i++) {
    individual_square_sum += ((A[i] * dp[i]) % mod);
  }
  return individual_square_sum;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll n;
  cin >> n;
  int arr[200001];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << findProductSum(arr, n) % mod;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll n;
  cin >> n;
  vector<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  ll sum = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += ((v[i] * v[j]) % mod);
    }
  }
  cout << sum % mod;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long ans = 0, sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    sum %= 1000000007LL;
  }

  for (int i = 0; i < n - 1; i++) {
    sum -= a[i];
    if (sum < 0)
      sum += 1000000007LL;
    ans += a[i] * sum;
    ans %= 1000000007LL;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  n = 1000000000;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    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 %= 1000000007LL;
    }
  }
  cout << ans << endl;
  return 0;
}