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

#include <iostream>
using namespace std;

#define ll long long
#define arrInput(arr, end)                                                     \\
  for (ll i = 0; i < end; i++)                                                 \\
    cin >> arr[i];

#define MOD 1000000007

ll fastpow(ll a, ll n, ll m) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = (res * a) % m;
    n >>= 1;
    a = (a * a) % m;
  }
  return res;
}

ll add(ll a, ll b, ll m) { return (a % m + b % m) % m; }

ll sub(ll a, ll b, ll m) { return ((a - b) % m + m) % m; }

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

ll div(ll a, ll b, ll m) { return mul(a, fastpow(b, m - 2, m), m); }

ll productSum(ll a[], ll n) {
  ll arSum = 0;
  for (int i = 0; i < n; i++)
    arSum = add(arSum, a[i], MOD);

  ll arSumSq = mul(arSum, arSum, MOD);

  ll indSqSum = 0;
  for (int i = 0; i < n; i++)
    indSqSum = add(indSqSum, mul(a[i], a[i], MOD), MOD);

  return div(sub(arSumSq, indSqSum, MOD), 2, MOD);
}

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

  ll arr[n];
  arrInput(arr, n);

  cout << productSum(arr, n) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define ll long long
#define arrInput(arr, start, end)                                              \\
  for (ll i = start; i < end; i++)                                             \\
    cin >> arr[i];

const int MOD = 1000000007;

ll product(ll arr[], ll n) {
  ll result = 0;
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++)
      result = (result + (arr[i] * arr[j])) % MOD;

  return result;
}

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

  ll arr[n];
  arrInput(arr, 0, n);

  cout << product(arr, n) << endl;

  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;                          // stdの省略
using pii = pair<int, int>;                   // pairの略記
using ll = long long;                         // long longの略記
using ull = unsigned long long;               // long longの略記

int main(void) {

  int n;
  cin >> n;

  vector<int> a(n);
  ll sum = 0;
  ll mod = pow(10, 9) + 7;
  rep(i, n) {
    cin >> a.at(i);
    sum += a.at(i);
    sum %= mod;
  }

  ll ans = 0;
  rep(i, n) {
    sum -= a.at(i);
    if (sum < 0)
      sum += mod;
    ans += a.at(i) * sum;
    ans %= 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) // マクロ
using namespace std;                          // stdの省略
using pii = pair<int, int>;                   // pairの略記
using ll = long long;                         // long longの略記

int main(void) {

  int n;
  cin >> n;

  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);

  ll sum = 0, e = pow(10, 9);
  rep(i, n - 1) {
    for (int j = i + 1; j < n; j++) {
      sum += (a.at(i) * a.at(j));
      if (sum >= (e + 7))
        sum %= (e + 7);
    }
  }
  cout << sum << endl;

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

#define MOD 1000000007
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    sum %= MOD;
  }
  ll ans = 0;
  rep(i, n - 1) {
    sum -= a[i];
    if (sum < 0) {
      sum += MOD;
    }
    ans += a[i] * sum;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

#define MOD 1000000007
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  ll ans = 0;
  rep(i, n - 1) {
    rep2(j, i + 1, n) {
      ans += a[i] * a[j];
      ans %= MOD;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  ll A, ans = 0, sumA = 0;
  while (N-- > 0) {
    cin >> A;
    ans = (ans + A * sumA) % (ll)(1e9 + 7);
    sumA = (sumA + A) % (ll)(1e9 + 7);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int N;
  cin >> N;
  ll m7 = 1000000007, ans = 0;
  vector<ll> A(N);
  for (auto &i : A)
    cin >> i;
  for (int i = 1; i < N; i++)
    for (int j = 0; j < i; j++)
      ans = (ans + A[i] * A[j]) % m7;
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

//| #######################|
//|  ADARSH SINGH KUSHWAHA |
//| ###################### |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Bahut_tej_gand_faad                                                    \\
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define frb(i, a, b) for (ll i = a - 1; i >= b; i--)
#define pb push_back
#define ff first
#define ss second
#define mp map<ll, ll>
#define mps map<char, ll>
#define v vector<ll>
#define srt(v) sort(v.begin(), v.end())
#define mod 1000000007
#define max 1000001
void solve() {
  ll n;
  cin >> n;
  ll sum = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    ans += (sum * a);
    ans = ans % mod;
    sum += a;
    sum = sum % mod;
  }
  cout << ans << endl;
}
int main() {
  Bahut_tej_gand_faad;

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  //}
  return 0;
}

Error: TLE

Fixed submission:

//| #######################|
//|  ADARSH SINGH KUSHWAHA |
//| ###################### |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Bahut_tej_gand_faad                                                    \\
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define frb(i, a, b) for (ll i = a - 1; i >= b; i--)
#define pb push_back
#define ff first
#define ss second
#define mp map<ll, ll>
#define mps map<char, ll>
#define v vector<ll>
#define srt(v) sort(v.begin(), v.end())
#define mod 1000000007
#define max 1000001
void solve() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      sum += (a[i] * a[j]) % mod;
      sum = sum % mod;
    }
  }
  cout << sum << endl;
}
int main() {
  Bahut_tej_gand_faad;
  // int t;
  //  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;

void printv(vector<ll> v) {
  for (int i = 0; i < v.size(); i++) {
    printf("%lld ", v[i]);
  }
  printf("\\n");
}

int main(void) {
  const ll M = 1000000007;
  ll n, ans = 0, temp = 0;
  cin >> n;
  vector<ll> a(n), v;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = n - 1; i > 0; i--) {
    temp += a[i];
    temp %= M;
    v.push_back(temp);
  }
  for (ll i = 0; i < n - 1; i++) {
    ans += a[i] * v[n - 2 - i];
    ans %= M;
  }
  // printv(a);
  // printv(v);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
unsigned long long int m = 1e9 + 7;
int main() {
  ll n, arr[200001];
  cin >> n;
  unsigned long long int sum = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    sum -= arr[i];
    ans = (ans + ((sum) % m * arr[i]) % m) % m;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  int mod = 1e9 + 7;
  while (t--) {
    int n;
    cin >> n;
    ll a[n];
    ll prefix[n] = {0};
    for (int i = 0; i < n; i++)
      cin >> a[i];
    prefix[1] = a[0];
    for (int i = 2; i < n; i++)
      prefix[i] += (prefix[i - 1] + a[i - 1]);
    ll sum = 0;
    for (int i = 1; i < n; i++)
      sum = ((sum % mod) + ((prefix[i] % mod) * (a[i] % mod)) % mod) % mod;
    cout << sum;
  }
}


Error: TLE

Fixed submission:

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

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

int main() {

  ll ans = 0;
  int n;
  cin >> n;
  vector<ll> a(200100);
  rep(i, n) cin >> a[i];

  ll sum = 0, b = 0, diag = 0, c = 0;
  for (int i = 0; i < n; i++)
    b = (b + a[i]) % 1000000007;
  for (int i = 0; i < n; i++)
    diag = (diag + (a[i] * a[i]) % 1000000007) % 1000000007;
  sum = (b * b) % 1000000007;
  c = (sum - diag + 1000000007) % 1000000007;
  if (c % 2 == 0)
    ans = c / 2;
  else
    ans = (c + 1000000007) / 2;

  cout << ans << endl;

  /*
  for(int j=0; j<n; j++){
      for(int i=0; i<j; i++){
          ans = (ans + (a[i]*a[j])%1000000007)%1000000007;
      }
  }
  */
}

Error: TLE

Fixed submission:

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

int main() {

  ll ans = 0;
  int n;
  cin >> n;
  vector<ll> a(200100);
  rep(i, n) cin >> a[i];

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

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

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

int main() {
  ll N;
  ll mod = 1000000007;
  cin >> N;
  vector<ll> A(N, 0);
  vector<ll> B(N + 1, 0);
  for (ll i = 0; i < N; i++) {
    cin >> A.at(i);
    B.at(i + 1) = B.at(i) + A.at(i);
  }
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    ll 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;
using ll = long long int;

int main() {
  ll N;
  cin >> N;
  vector<ll> V(N);
  for (int i = 0; i < N; i++) {
    cin >> V.at(i);
    V.at(i) = V.at(i) % 1000000007;
  }

  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += (V.at(i) * V.at(j)) % 1000000007;
    }
  }

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

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

const ll MM = 1e9 + 7;
int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll &i : A)
    cin >> i;
  ll sum = 0, count = 0;
  vector<ll> B(N);

  for (ll j = N - 1; j >= 0; j--) {
    //  cout << j << endl;
    sum += A[j] % MM;
    sum %= MM;
    B[j] = sum;
  }

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

  cout << count << endl;
}


Error: TLE

Fixed submission:

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

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

#include <iostream>

using namespace std;

int main() {
  const int MAX = 2e5;
  const long long MOD = 1e9 + 7;
  const long long MODINV = 500000004;

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

  long long sum = 0;
  for (int i = 0; i < N; i++) {
    sum = (sum + A[i]) % MOD;
  }

  long long sqr = (sum * sum) % MOD;

  long long mid = 0;
  for (int i = 0; i < N; i++) {
    mid = mid + ((A[i] * A[i]) % MOD) % MOD;
  }

  long long top = (sqr - mid) % MOD;
  if (top < 0)
    top += MOD;

  long long ans = (top * MODINV) % MOD;

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  const int MAX = 2e5;
  const int MOD = 1e9 + 7;
  int N;
  cin >> N;
  long long A[MAX];
  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]) % MOD;
      ans %= MOD;
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

#define PI 3.14159265358979323846264338327950L

int main(void) {
  long long tmp, ans, N, wa = 0;
  long long MOD = 1000000007;

  cin >> N;
  vector<long long> A(N + 5, 0), sum(N + 5, 0);

  wa = 0;
  for (int i = 1; i <= N; i++) {
    cin >> tmp;
    tmp = tmp % MOD;
    A[i] = tmp;

    wa = wa + tmp;
    sum[i] = wa;
  }

  ans = 0;
  for (int i = 0; i < N; i++) {
    tmp = sum[N] - sum[i];
    tmp = tmp % MOD;
    tmp = A[i] * tmp;
    tmp = tmp % MOD;
    ans = ans + tmp;
    ans = ans % MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define PI 3.14159265358979323846264338327950L

int main(void) {
  long long tmp, ans, N;
  long long MOD = 1e9 + 7;

  cin >> N;
  vector<long long> A(N + 1, 0);
  // map<long long,long long> mp;
  // vector<long long> vec(MOD+5);
  // set<long long> S;

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

  ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      tmp = A[i] * A[j];
      tmp = tmp % MOD;

      ans = ans + tmp;
      ans = ans % MOD;

      // vec[tmp]++;
      // S.insert(tmp);
    }
  }

  // ans=0;
  // for(auto itr:S){
  //     ans=ans+(itr*(vec[itr]%MOD)%MOD);
  //     ans=ans%MOD;
  // }

  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define mod (1000000007)
using namespace std;
int main() {

  int N;
  cin >> N;
  vector<long long> A(N);
  long long ans = 0;
  long long allsum = 0;
  for (int i = 0; i <= N - 1; i++) {

    cin >> A[i];
    allsum += A[i];
    allsum %= mod;
  }
  for (int i = 0; i <= N - 2; i++) {

    allsum -= A[i];
    if (allsum < 0)
      allsum += mod;
    ans += allsum * A[i];
    ans %= mod;
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
#define mod (1000000007)
using namespace std;
int main() {

  int N;
  cin >> N;
  vector<long long> A(N);
  long long ans = 0;
  for (int i = 0; i <= N - 1; i++) {

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

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

      ans += A[i] * A[j];
      ans = ans % mod;
    }
  }
  cout << ans;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define lli long long int

using namespace std;

int main() {
  lli n;
  int mod = 1000000007;
  std::cin >> n;
  std::vector<lli> v;
  lli sum = 0;
  for (lli i = 0; i < n; ++i) {
    int f;
    std::cin >> f;
    v.push_back(f);
    sum += f;
    sum %= mod;
  }
  lli ans = 0;
  for (int i = 0; i < (int)v.size(); ++i) {
    sum -= v[i];
    if (sum < 0)
      sum += mod;
    ans += v[i] * sum;
    ans %= mod;
  }
  std::cout << ans << std::endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int mod = 1e9 + 7;
  int N;
  cin >> N;
  vector<long long> arr;
  for (int i = 0; i < N; ++i) {
    int x;
    std::cin >> x;
    arr.push_back(x);
  }
  long long sum = 0;
  for (int i = 0; i < arr.size(); ++i) {
    for (int j = i + 1; j < arr.size(); ++j) {
      long long val = (arr[i] * arr[j]) % mod;
      sum = (sum + val) % mod;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long
#define ld long double

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rrep(i, a, b) for (ll i = a; i >= b; --i)

#define pii pair<int, int>
#define pll pair<ll, ll>

#define fi first
#define se second

#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()

#define endl '\\n'
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double PI = acos(-1);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};

const int MAX = 1000010;

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

  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define ll long long
#define ld long double

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rrep(i, a, b) for (ll i = a; i >= b; --i)

#define pii pair<int, int>
#define pll pair<ll, ll>

#define fi first
#define se second

#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()

#define endl '\\n'
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int MOD = 1e9 + 7;
const ll INF = 1e18;
const double PI = acos(-1);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};

const int MAX = 510000;

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

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

  ll sum = 0;
  rep(i, n) {
    sum += a[i];
    sum %= MOD;
  }
  ll ans = 0;
  rep(i, n) {
    ans += a[i] * sum;
    ans %= MOD;
  }
  rep(i, n) {
    ans -= a[i] * a[i];
    while (ans < 0)
      ans += MOD;
    ans %= MOD;
  }
  while (ans < 0)
    ans += MOD;
  ans /= 2;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define for(a, b) for (auto i = (a); i < (b); i++)
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0, modo = 1e9 + 7;
  for (0, n) {
    cin >> a[i];
    sum += a[i];
    sum %= modo;
  }
  ll ans = 0;
  for (0, n) {
    sum -= a[i];
    if (sum < 0) {
      sum += modo;
    }
    ans += a[i] * sum;
    ans %= modo;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<long long> a(100001), b(100001, -1);
const long long mod = 1e9 + 7;
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;

  for (auto i = 1; i <= n; i++) {
    cin >> a[i];
  }

  long long ans = 0;
  for (auto i = 1; i <= (n - 1); i++) {
    for (auto j = i + 1; j <= n; j++) {
      ans += (a[i] * a[j]) % mod;
      ans %= mod;
    }
  }
  cout << ans;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define _repi(i, s, n) for (ll i = (ll)(s); i < (ll)n; i++)
#define REP(...) _overload(__VA_ARGS__, _repi, _rep, )(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define UNIQUE(v) (v).erase(unique(all(v)), (v).end());
#define MOD 1000000007

int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll ans = 0;
  ll sum = 0;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
  }

  REP(i, n) {
    sum -= a[i];
    ans += sum % MOD * a[i];
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define _repi(i, s, n) for (ll i = (ll)(s); i < (ll)n; i++)
#define REP(...) _overload(__VA_ARGS__, _repi, _rep, )(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define UNIQUE(v) (v).erase(unique(all(v)), (v).end());
#define MOD 1000000007

int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll ans = 0;
  REP(i, n) { cin >> a[i]; }

  REP(i, n) {
    REP(j, i + 1, n) {
      ans += a[i] * a[j];
      ans %= MOD;
    }
  }

  cout << ans << endl;

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

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

const long long MOD = 1000000007;

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

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


Error: TLE

Fixed submission:

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

const long long MOD = 1000000007;

int main() {
  int n;
  long long sum = 0;
  vector<long long> a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0; i < (n - 1); i++) {
    for (int j = i + 1; j < n; j++) {
      sum += (a[i] * a[j] % MOD);
      sum = sum % MOD;
    }
  }

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