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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, o) for (ll i = 0; i < (o); i++)
#define rep_s(i, o) for (int i = 1; i <= (o); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) < (int)(b); i++)
#define NUM 1e5

typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    return b;
  }
  return a;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return a;
  }
  return a;
}

const int MOD = 1000000007;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n), s(n + 1);

  rep(i, n) {
    cin >> a[i];
    s[i + 1] = s[i] + a[i];
  }

  ll ans = 0;
  rep(i, n) {
    ll sum = (s[n] - s[i + 1]) % MOD;
    ans += a[i] * sum;
    ans = ans % MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, o) for (ll i = 0; i < (o); i++)
#define rep_s(i, o) for (int i = 1; i <= (o); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) < (int)(b); i++)
#define NUM 1e5

typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    return b;
  }
  return a;
}

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return a;
  }
  return a;
}

const int MOD = 1000000007;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  // rep(i, n) cout << a[i];

  ll sum = 0;
  rep(i, n - 1) {
    for (ll j = i + 1; j < n; j++) {
      sum += (a[i] * a[j]) % MOD;
      sum = sum % MOD;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7;
ll a[200005];

int main() {
  ll sum = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum = (sum + a[i]) % MOD;
  }
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    sum -= a[i];
    if (sum < 0)
      sum += MOD;
    ans = (ans + sum * a[i]) % MOD;
  }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7;
ll a[100005];

int main() {
  ll sum = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    sum -= a[i];
    ans = (ans + sum * a[i]) % MOD;
  }
  cout << ans << "\\n";
  return 0;
}
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 p = 1000000007, ans = 0;
  long long int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long int x = 0;
  for (int i = 0; i < n; i++) {
    ans = (ans + (arr[i] * x)) % p;
    x = (x + arr[i]) % p;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  unsigned long long int n;
  cin >> n;
  unsigned long long int p = 1000000007, ans = 0;
  unsigned long long int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  unsigned long long int s1 = 0, s2 = 0, s3 = 1;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = ans + (arr[i] * arr[j]);
      ans = ans % p;
    }
  }

  ans = ans % p;

  cout << ans << endl;

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

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

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

  vector<ll> a(n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    sum += a.at(i);
  }

  // b[0] = (1+2+...+n-1)
  // b[1] = (2+3+...+n-1)
  // b[n-1] = 0
  vector<ll> b(n);
  b.at(0) = sum - a.at(0);
  for (int i = 1; i < n; i++) {
    b.at(i) = b.at(i - 1) - a.at(i);
  }

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    ll w = b.at(i) % 1000000007;
    ll x = a.at(i) * w;
    // cout << i << " " << a.at(i) << " " << b.at(i) << " " << x << endl;
    ll y = x % 1000000007;
    ans += y;
  }

  ans = ans % 1000000007;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }

  ll ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; j++) {
      ll x = a.at(i) * a.at(j);
      ll r = x % 1000000007;
      ans += r;
    }
  }

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

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  ll tmp = 0;
  ll mod = 1000000007;
  ll sum = accumulate(A.begin(), A.end(), 0LL);
  ll ans = 0;
  for (ll i = 0; i < N - 1; i++) {
    sum = sum - A.at(i);
    tmp = sum % mod;
    tmp = tmp * A.at(i);
    tmp = tmp % mod;
    ans = ans + tmp;
    ans %= mod;
  }
  // for(ll i=0;i<N-1;i++){
  //     for(ll j=i+1;j<N;j++){
  //         ans += A.at(i)*A.at(j);
  //         ans=ans%mod;
  //     }
  // }
  cout << ans;
}

Error: TLE

Fixed submission:

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

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  ll tmp = 0;
  ll mod = 1000000007;
  // ll sum = accumulate(A.begin(),A.end(),0LL);
  ll ans = 0;
  // for(ll i = 0;i<N-1;i++){
  //     sum = sum-A.at(i);
  //     tmp = sum*A.at(i);
  //     tmp = tmp%mod;
  //     ans = ans + tmp;
  //     ans %=mod;
  // }
  for (ll i = 0; i < N - 1; i++) {
    for (ll j = i + 1; j < N; j++) {
      ans += A.at(i) * A.at(j);
      ans = ans % mod;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long inf = 1e9 + 7;
  int N;
  cin >> N;
  vector<long long int> A(N);
  long long int sumA = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] = A[i] % inf;
    sumA = sumA + A[i];
    sumA = sumA % inf;
  }
  long long int sumanijo = 0;
  sumanijo = sumA * sumA % inf;
  long long int nijosum = 0;
  for (int i = 0; i < N; i++) {
    nijosum = nijosum + A[i] * A[i] % inf;
    nijosum = nijosum % inf;
  }
  long long int sum = 0;
  long long int sa = 0;
  sa = sumanijo - nijosum;
  while (true) {
    if (sa < 0) {
      sa = sa + inf;
    } else {
      break;
    }
  }
  sum = sa % inf;
  if (sum % 2 == 1) {
    sum = (sum + inf) / 2;
  } else {
    sum = sum / 2;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long inf = 1e9 + 7;
  int N;
  cin >> N;
  vector<long long int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] = A[i] % inf;
  }
  long long int sum = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N && i > j; j++) {
      sum = sum + (A[i] * A[j] % inf);
      sum = sum % inf;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

constexpr long long MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  int ans = 0ll;
  cin >> a[0];

  for (int i = 1; i < n; i++) {
    cin >> a[i];
    ans += (a[i] * a[i - 1]) % MOD;
    a[i] += a[i - 1];
    ans %= MOD;
    a[i] %= MOD;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

constexpr long long MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long ans = 0ll;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = 0; j < i; j++) {
      ans += (a[j] * a[i]) % MOD;
      ans %= MOD;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second

#define pb push_back
#define es erase
#define in insert

#define pii pair<ll, ll>
#define ll long long
#define lb long double

#define ioss                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define m_p(i, j) make_pair(i, j)
#define mem(a, x) memset(a, x, sizeof(a))
#define endl "\\n"
#define all(x) x.begin(), x.end()

const ll mod = 1e9 + 7;
const ll maxn = 6e5 + 7;
ll n, a[maxn];
int main() {
  ll sum = 0, tot = 0;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i], sum += a[i], sum %= mod, tot += a[i] * a[i], tot %= mod;
  sum *= sum;
  sum %= mod;
  sum -= tot;
  sum += mod;
  sum %= mod;
  if (sum % 2 == 0)
    cout << sum / 2 << endl;
  else {
    sum += mod;
    cout << sum / 2 << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second

#define pb push_back
#define es erase
#define in insert

#define pii pair<int, int>
#define ll long long
#define lb long double

#define ioss                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define m_p(i, j) make_pair(i, j)
#define mem(a, x) memset(a, x, sizeof(a))
#define endl "\\n"
#define all(x) x.begin(), x.end()

const int mod = 1e9 + 7;
const int maxn = 1e5 + 7;
ll n, a[maxn];
int main() {
  ll sum = 0, tot = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], sum += a[i], sum %= mod, tot += a[i] * a[i], tot %= mod;
  sum *= sum;
  sum %= mod;
  sum -= tot;
  sum += mod;
  sum %= mod;
  cout << sum / 2 << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long int sum = 0;
  long long int res = 0;
  int mod = 1000000007;
  int n;

  cin >> n;
  vector<int> num(n);

  for (int i = 0; i < n; i++) {
    cin >> num[i];
    sum += num[i];
    sum %= mod;
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    sum -= num[i];
    if (sum < 0)
      sum += mod;
    ans += sum * num[i];
    ans %= mod;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

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

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

ll power(ll x, ll p) {

  ll res = 1;
  while (p) {
    if (p & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    p >>= 1;
  }
  return res;
}

ll f(ll A[], int n) {

  ll array_sum = 0;
  for (int i = 0; i < n; i++)
    array_sum = (array_sum % mod + A[i] % mod) % mod;

  ll sq = (array_sum * array_sum) % mod;

  ll in = 0;
  for (int i = 0; i < n; i++)
    in = (in + (A[i] * A[i]) % mod) % mod;

  ll rs = (((sq - in + mod) % mod) * (power(2, mod - 2) % mod)) % mod;

  return rs;
}

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long mo = 1e9 + 7;
int main(void) {
  int n;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long sum = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      a[i] %= mo;
      a[j] %= mo;
      long p = (a[i] * a[j]) % mo;
      sum += p;
      sum %= mo;
    }
  cout << sum;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
const ll m = 1e9 + 7;
using namespace std;
ll add(ll i, ll j) { return ((i % m) + (j % m)) % m; }
ll mod(ll i, ll j) { return ((i % m) * (j % m)) % m; }
int main() {
  int n;
  cin >> n;
  vector<ll> v(n + 1, 0);
  vector<ll> presum(n + 1, 0);
  v[n] = 0;
  presum[n] = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    v[i] = v[i] % m;
  }
  for (int i = n - 1; i >= 0; i--) {
    presum[i] = add(presum[i + 1], v[i + 1]);
  }
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans += mod(presum[i], v[i]);
    ans %= m;
  }
  cout << ans << endl;
}

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 <bits/stdc++.h>
#define ll long long
const ll m = 1e9 + 7;
using namespace std;
ll add(ll i, ll j) { return ((i % m) + (j % m)) % m; }
ll mod(ll i, ll j) { return ((i % m) * (j % m)) % m; }
int main() {
  int n;
  cin >> n;
  vector<ll> v(n);
  vector<ll> presum(n);
  v[n] = 0;
  presum[n] = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    v[i] = v[i] % m;
  }
  for (int i = n - 1; i >= 0; i--) {
    presum[i] = add(presum[i + 1], v[i + 1]);
  }
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans += mod(presum[i], v[i]);
    ans %= m;
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define INF ~(1 << 31)
#define INFL ~(1LL << 63)
#define mod 1000000007LL

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum = (sum + a[i]) % mod;
  }

  ll ans = 0;
  rep(i, n) {
    if (sum - a[i] < 0)
      sum += mod;
    ans = (ans + (sum - a[i]) * a[i] % mod) % mod;
    sum -= a[i];
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rreps(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define INF ~(1 << 31)
#define INFL ~(1LL << 63)
#define mod 1000000007LL

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  ll ans = 0;
  rep(i, n) {
    cin >> a[i];
    rep(j, i) { ans = (ans + a[i] * a[j]) % 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++)

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

  vector<long> A(N);
  rep(i, N) cin >> A.at(i);

  long ans = 0;

  long ai;
  long mod = pow(10, 9) + 7;

  long as = 0;
  for (int i = N - 2; i >= 0; i--) {
    ai = A.at(i);
    as += A.at(i + 1);
    as %= mod;
    ans += (ai * as) % mod;
  }

  cout << ans % mod << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

  vector<long> A(N);
  rep(i, N) cin >> A.at(i);

  long ans = 0;

  long ai;
  long aj;
  long mod = pow(10, 9) + 7;
  for (int i = 0; i < N - 1; i++) {
    ai = A.at(i);
    for (int j = i + 1; j < N; j++) {
      aj = A.at(j);
      ans += (ai * aj) % mod;
    }
  }

  cout << ans % mod << endl;

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

#include <algorithm> //sort(a.begin(), a.end());reverse(a.begin(), a.end());
#include <bits/stdc++.h>
#include <iostream>
#include <math.h> //pow(x,y)=x^y 99999とかの場合+1するif文
#define rep(init, i, n) for (int i = init; i < (n); i++)
#define ll long long
#define rev(s) reverse(s.begin(), s.end())
#define sor(v) sort(v.begin(), v.end())
// コード自動整形 Shift + Option + F @vscode
using namespace std;

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

  vector<ll> A(N);
  vector<ll> summod(N - 1);

  rep(0, i, N) { cin >> A.at(i); }

  ll output = 0;
  ll modbs = 1000000007;

  summod.at(N - 2) = A.at(N - 1);

  for (int i = N - 3; i >= 0; --i) {
    summod.at(i) = summod.at(i + 1) + A.at(i + 1);
    // cout<<"summod1.at("<<i<<")"<<summod.at(i)<<endl;//
    summod.at(i) %= modbs;
  }
  // rep(0,i,N-1){//
  // cout<<"summod.at("<<i<<")"<<summod.at(i)<<endl;//
  //}//

  rep(0, i, N - 1) {
    output += A.at(i) * summod.at(i);
    // cout<<"output"<<output<<endl;//
    output %= modbs;
    // cout<<"outputmoded"<<output<<endl;//
  }

  cout << output << endl;
}
/////main/////

/////function/////

Error: WA

Fixed submission:

#include <algorithm> //sort(a.begin(), a.end());reverse(a.begin(), a.end());
#include <bits/stdc++.h>
#include <iostream>
#include <math.h> //pow(x,y)=x^y 99999とかの場合+1するif文
#define rep(init, i, n) for (int i = init; i < (n); i++)
#define ll long long
#define rev(s) reverse(s.begin(), s.end())
#define sor(v) sort(v.begin(), v.end())
// コード自動整形 Shift + Option + F @vscode
using namespace std;

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

  vector<ll> A(N);
  vector<ll> summod(N - 1);

  rep(0, i, N) { cin >> A.at(i); }

  ll output = 0;
  ll modbs = 1000000007;

  summod.at(N - 2) = A.at(N - 1);

  for (int i = N - 3; i < 0; i--) {
    summod.at(i) = summod.at(i + 1) + A.at(i + 1);
    // cout<<"summod1.at("<<i<<")"<<summod.at(i)<<endl;//
    summod.at(i) %= modbs;
  }
  // rep(0,i,N-1){
  // cout<<"summod.at("<<i<<")"<<summod.at(i)<<endl;//
  //}

  rep(0, i, N - 1) {
    output += A.at(i) * summod.at(i);
    // cout<<"output"<<output<<endl;//
    output %= modbs;
    // cout<<"outputmoded"<<output<<endl;//
  }

  cout << output << endl;
}
/////main/////

/////function/////
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

// Most important thing in CP - Have Fun :)

ll binpow(ll a, ll p) {
  ll res = 1;
  while (p > 0) {
    if (p & 1)
      res = (res * a) % hell;
    a = (a * a) % hell;
    p = p >> 1;
  }
  return res;
}

void solve() {
  // write here
  ll n;
  cin >> n;
  vector<ll> arr(n);
  rep(i, 0, n) cin >> arr[i];
  ll arr_sum = 0;
  rep(i, 0, n) arr_sum = (arr_sum + arr[i]) % hell;
  ll arr_sq_sum = (arr_sum * arr_sum) % hell;
  ll single_sq_sum = 0;
  rep(i, 0, n) single_sq_sum = (single_sq_sum + arr[i] * arr[i]) % hell;
  ll val =
      ((arr_sq_sum - single_sq_sum + hell) % hell * binpow(2, hell - 2)) % hell;
  cout << val << endl;
  return;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  // insert code

  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

// Most important thing in CP - Have Fun :)

void solve() {
  // write here
  ll n;
  cin >> n;
  vector<ll> arr(n);
  rep(i, 0, n) cin >> arr[i];
  ll ans = 0;
  rep(i, 0, n) {
    rep(j, i + 1, n) { ans = (ans + arr[i] * arr[j]) % hell; }
  }
  cout << ans << endl;
  return;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  // insert code

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

// In the nAme Of Allah
#include <bits/stdc++.h>
#define pb push_back
#define ins insert
#define f first
#define pp pop_back
#define ll long long int
#define fst                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
const ll mod = 1e9 + 7;
using namespace std;
ll n, a[200005], s = 0, p = 0;
int main() {
  fst;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    p += (a[i] % mod) * (a[i] % mod) % mod;
    s += (a[i] % mod);
    s %= mod;
    p %= mod;
  }
  if (((s * s) - p) % 2 == 0) {
    cout << (((s * s) - p) / 2) % mod;
  } else {
    cout << (((s * s) - p + mod) / 2) % mod;
  }
  return 0;
}


Error: TLE

Fixed submission:

// In the nAme Of Allah
#include <bits/stdc++.h>
#define pb push_back
#define ins insert
#define f first
#define pp pop_back
#define ll long long int
#define fst                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
const ll mod = 1e9 + 7;
using namespace std;
ll n, a[200005], s = 0;
int main() {
  fst;
  cin >> n >> a[0];
  for (ll i = 1; i < n; i++) {
    cin >> a[i];
    for (ll j = i - 1; 0 <= j; j--) {
      s += (a[i] % mod) * (a[j] % mod);
      s = s % mod;
    }
  }
  cout << s;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int mod = 1000000007;
  int N;
  cin >> N;

  long long int *a = new long long int[N];
  long long int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += a[i];
    sum %= mod;
  }

  long long int ans = 0;

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

  cout << ans << endl;

  delete[] a;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  long long int *a = new long long int[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  long long int 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;

  delete[] a;

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

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

int main() {
  ll int n, i, s = 0, sum = 0, p;
  cin >> n;
  ll int arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }

  for (i = 0; i < n - 1; i++) {
    s = (s + arr[i]) % 1000000007;
    p = (arr[i + 1] * s) % 1000000007;
    sum = (sum + p) % 1000000007;
  }
  cout << sum;
}

Error: TLE

Fixed submission:

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

int main() {
  ll int n, i, j, s = 0, sum = 0, p;
  cin >> n;
  ll int arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }

  for (i = n - 1; i > 0; i++) {
    s += arr[i];
    p = arr[i - 1] * s;
    sum += p;
  }
  cout << (sum % 1000000007);
}