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

#include <iostream>
using namespace std;
using ll = long long;
#define MOD 1000000007;

int main() {
  ll N;
  ll A[200002];
  cin >> N;
  ll sum = 0;
  ll res = 0;
  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
    sum += A[i];
    sum %= MOD;
  }
  for (ll i = 1; i < N; i++) {
    sum -= A[i];
    if (sum < 0) {
      sum += MOD;
    }
    res += (A[i] * sum) % MOD;
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
using ll = long long;
#define MOD 1000000007;

int main() {
  ll N;
  ll A[N + 1];
  cin >> N;
  ll sum = 0;
  ll res = 0;
  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
    sum += A[i];
    sum %= MOD;
  }
  for (ll i = 1; i < N; i++) {
    sum -= A[i];
    if (sum < 0) {
      sum += MOD;
    }
    res += (A[i] * sum) % MOD;
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const ll mod = 1e9 + 7;
#define vi vector<int>
#define pb emplace_back
ll poww(ll n, ll x) {
  ll res = 1;

  while (x > 0) {
    if (x & 1)
      res *= n;

    x = x >> 1;
    n *= n;
  }
  return res;
}

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

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

  ll *pref = new ll[n + 1];
  pref[n + 1] = 0;
  for (int i = n - 1; i >= 0; i--) {
    pref[i] = (pref[i + 1] + a[i]) % mod;
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += (a[i] * pref[i + 1]) % mod;
  }
  cout << sum % mod;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const ll mod = 1e9 + 7;
#define vi vector<int>
#define pb emplace_back
ll poww(ll n, ll x) {
  ll res = 1;

  while (x > 0) {
    if (x & 1)
      res *= n;

    x = x >> 1;
    n *= n;
  }
  return res;
}

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

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

  ll *pref = new ll[n + 1];
  pref[0] = a[0];
  for (int i = 1; i < n; i++) {
    pref[i] = ((pref[i - 1]) % mod * a[i] % mod) % mod;
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      a[i] %= mod;
      a[j] %= mod;
      sum += (a[i] * a[j]) % mod;
    }
  }
  cout << sum % mod;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define print cout <<
#define printl(s) cout << s << endl;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

const ll modseed = pow(10, 9) + 7;

template <typename T> vector<T> getValues(int num) {
  vector<T> values(num);
  for (int i = 0; i < num; i++) {
    cin >> values.at(i);
  }
  return values;
}

ll calculateMod(ll n, ll expo, ll modSeed) {
  ll ret = 1;
  rep(i, expo) {
    ret *= n;
    ret = ret % modSeed;
  }
  return ret;
}

int main() {
  ll N;
  cin >> N;
  vector<ll> inputs(N);
  vector<ll> s(N + 1, 0);
  rep(i, N) {
    cin >> inputs.at(i);
    s.at(i + 1) = s.at(i) + inputs.at(i);
  }

  ll ans = 0;
  rep(i, N) {
    ll sum = (s.at(N) - s.at(i + 1)) % modseed;
    ans += sum * inputs[i];
    ans %= modseed;
  }

  printl(ans);
}


Error: TLE

Fixed submission:

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

#define print cout <<
#define printl(s) cout << s << endl;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

const ll modseed = pow(10, 9) + 7;

template <typename T> vector<T> getValues(int num) {
  vector<T> values(num);
  for (int i = 0; i < num; i++) {
    cin >> values.at(i);
  }
  return values;
}

ll calculateMod(ll n, ll expo, ll modSeed) {
  ll ret = 1;
  rep(i, expo) {
    ret *= n;
    ret = ret % modSeed;
  }
  return ret;
}

int main() {
  ll N;
  cin >> N;
  auto inputs = getValues<ll>(N);
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int g = i + 1; g < N; g++) {
      ll result_1 = inputs.at(i) % modseed;
      ll result_2 = inputs.at(g) % modseed;
      ll mo = (result_1 * result_2) % modseed;
      ans += mo;
    }
  }
  ans %= modseed;
  printl(ans);
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int N;
  scanf("%d", &N);

  vector<long long> v(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &v[i]);
  }

  long long S = 0;
  long long MOD = pow(10, 9) + 7;

  vector<long long> CUMSUM(N);
  CUMSUM[N - 1] = v[N - 1];
  for (int i = N - 2; i >= 0; i--) {
    CUMSUM[i] = (v[i] + CUMSUM[i + 1]) % MOD;
  }

  for (int i = 0; i < N - 1; i++) {
    S += v[i] * CUMSUM[i + 1] % MOD;
  }

  S %= MOD;

  printf("%d\\n", S);

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  int N;
  scanf("%d", &N);

  vector<long long> v(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &v[i]);
  }

  long long S = 0;
  long long XX = pow(10, 9) + 7;

  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      S += v[i] % XX * v[j] % XX;
      S %= XX;
    }
  }

  printf("%d\\n", S);

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <vector>

using namespace std;

const int MOD = 1000000007;

int main() {
  int n;
  long long sum = 0;
  cin >> n;
  long long arr[n + 1];
  long long arrsum = 0;

  for (int i = 0; i < n; i++) {
    long long num;
    cin >> num;
    arr[i] = num;
    arrsum += num;
  }

  for (int i = 0; i < n - 1; i++) {
    arrsum -= arr[i];
    int arrsummod = arrsum % MOD;
    int result = arr[i] * arrsummod % MOD;
    sum += result;
    sum %= MOD;
  }
  cout << sum % MOD << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <vector>

using namespace std;

const int MOD = 1000000007;

int main() {
  int n;
  long long sum = 0;
  cin >> n;
  long long arr[n + 1];

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

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int result = arr[i] * arr[j] % MOD;
      sum += result;
      sum %= MOD;
    }
  }
  cout << sum % MOD << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
// cout << setprecision(20) <<

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

  ll sum = 0;
  for (int i = 0; i < n - 1; ++i) {
    ll tmp = a[n] - a[i + 1];
    tmp %= MOD;
    sum += tmp * v[i];
    sum %= MOD;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
// cout << setprecision(20) <<

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

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 1000000007;
ll mod(ll x) { return ((x % M + M) % M); }
ll add(ll a, ll b) { return mod(mod(a) + mod(b)); }
ll mul(ll a, ll b) { return mod(mod(a) * mod(b)); }
ll minu(ll a, ll b) { return mod(mod(a) - mod(b)); }
ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mul(res, a);
    b = b >> 1;
    a = mul(a, a);
  }
  return res;
}
int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll s = 0;
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
    s = add(s, a[i]);
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    s = minu(s, a[i]);
    ll val = mul(a[i], s);
    ans = add(ans, val);
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 1000000007;
ll mod(ll x) { return ((x % M + M) % M); }
ll add(ll a, ll b) { return mod(mod(a) + mod(b)); }
ll mul(ll a, ll b) { return mod(mod(a) * mod(b)); }
ll minu(ll a, ll b) { return mod(mod(a) - mod(b)); }
ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = mul(res, a);
    b = b >> 1;
    a = mul(a, a);
  }
  return res;
}
int main() {
  ll n;
  cin >> n;
  int a[n];
  for (ll i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll s = 0;
    for (ll j = i + 1; j < n; j++) {
      s = add(s, mul(a[i], a[j]));
    }
    ans = add(ans, s);
  }
  cout << ans;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>

int main() {
  int a;
  long long int sum = 0, x = 0;
  long long int arr[200005];
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    scanf("%lld", &arr[i]);
  }
  for (int j = 0; j < a; j++) {
    sum = (sum + x * arr[j]) % 1000000007;
    x = (x + arr[j]) % 1000000007;
  }
  printf("%lld", sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

int main() {
  long long int sum = 0, arr[200005] = {0};
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &arr[i]);
  }
  for (int j = 0; j < n - 1; j++) {
    for (int k = j + 1; k < n; k++) {
      sum += arr[j] * arr[k];
      if (sum > 1000000007) {
        sum %= 1000000007;
      }
    }
  }
  printf("%lld\\n", sum);
  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 dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const ll INF = 1e18;
const ll mod = 1e9 + 7;

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

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 dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const ll INF = 1e18;
const ll mod = 1e9 + 7;

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

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int mod = 1000000007;
  vector<long long> B(N + 1, 0);
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    B[i + 1] = B[i] + A[i];
  }
  for (int i = 0; i < N; i++) {
    long long sum = (B[N] - B[i + 1]) % mod;
    ans += A[i] * sum;

    ans %= mod;
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

int main() {
  long long a = 0, d = 0;
  std::cin >> a;
  long long b[10000000000];
  for (long long i = 0; i < a; i++) {
    std::cin >> b[i];
  }
  for (long long i = 0; i < a; i++) {

    for (long long j = 0; j < a; j++) {
      d += b[i] * b[j];
    }
  }
  std::cout << d << std::endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll p = 1e9 + 7;
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[n - 1 - i];
  }
  vector<ll> ruiseki(n + 1, 0);
  for (ll i = 0; i < n; i++) {
    ruiseki[i + 1] = a[i] + ruiseki[i];
    ruiseki[i + 1] %= p;
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans += (ruiseki[i] * a[i]) % p;
    ans %= p;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using ll = long long;
using ull = unsigned long long int;
const int mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ull> suff(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  suff[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; --i) {
    suff[i] = (suff[i + 1] % mod + a[i] % mod) % mod;
  }
  ull ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans = (ans + ((suff[i + 1] % mod) * (a[i] % mod)) % mod) % mod;
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
typedef long long ll;
using ull = unsigned long long int;
typedef pair<int, int> pie;
const int mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 0)
      continue;
    for (int j = i + 1; j < n; j++) {
      ans = (ans + (a[i] * a[j]) % mod) % mod;
    }
  }
  cout << ans % mod;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
#define F first
#define S second
#define ll long long
const int MOD = 1e9 + 7;
const float epsilon = 0.0005f;
const float PI = 3.1415926535897932384626433f;

vector<pair<int, int>> dire{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

// std::cout << std::fixed;
// std::cout << std::setprecision(9);

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // int t;cin>>t;
  // for(int i = 0;i<t;++i){
  //     solve();
  // }
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
#define F first
#define S second
#define ll long long
const int MOD = 1e9 + 7;
const float epsilon = 0.0005f;
const float PI = 3.1415926535897932384626433f;

vector<pair<int, int>> dire{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

// std::cout << std::fixed;
// std::cout << std::setprecision(9);

void solve() {
  int n;
  cin >> n;
  vector<ll> a(n, 0);
  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 c = a[i] * a[j] % MOD;
      ans = ans + c % MOD;
      ans = ans % MOD;
    }
  }
  cout << ans << "\\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // int t;cin>>t;
  // for(int i = 0;i<t;++i){
  //     solve();
  // }
  solve();
  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++)
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;

ll M = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  // vector<vector<ll>> As(N, vector<ll>(N));
  vector<ll> Sums(N);
  ll ans = 0;
  rep(i, N) { cin >> A.at(i); }

  for (int i = N - 2; i >= 0; --i) {
    Sums.at(i) = (Sums.at(i + 1) + A.at(i + 1)) % M;
  }

  rep(i, N) { ans = (ans + Sums.at(i) * A.at(i)) % M; }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;

ll M = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  // vector<vector<ll>> As(N, vector<ll>(N));

  ll ans = 0;
  rep(i, N) {
    cin >> A.at(i);
    rep(j, i) {
      if (i != 0)
        ans = (ans + A.at(i) * A.at(j)) % M;
    }
  }
  /*
    rep(i,N){
      rep(j,i){
        ans = (ans + As.at(j).at(i)) % M;
      }
    }
    */
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, i, mod = 1000000007;
  long long ans = 0, sum = 0;
  cin >> N;
  vector<long long> A(N);
  for (i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }
  for (i = 0; i <= N - 2; i++) {
    sum -= A[i];
    ans += A[i] * (sum % mod);
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

#define _GLIBCXX_DEBUG
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define p_b push_back
#define e_b emplace_back
#define m_p make_pair
#define m_t make_tuple

using namespace std;

int main() {
  ll md = 1000000007, ans = 0, sum = 0;
  int n;
  cin >> n;
  vector<ll> vec(n);

  rep(i, n) {
    cin >> vec[i];
    sum += vec[i];
    sum %= md;
  }

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

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

#define _GLIBCXX_DEBUG
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define p_b push_back
#define e_b emplace_back
#define m_p make_pair
#define m_t make_tuple

using namespace std;

int main() {
  ll md = 1000000007, ans = 0;
  int n;
  cin >> n;
  vector<ll> vec(n);

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

  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      ans += ((vec[i] * vec[j]) % md);
    }
  }

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

#include <iostream>
#include <vector>
using namespace std;

int main() {
  auto &in = cin;

  int N;
  in >> N;
  long long sum = 0;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i) {
    in >> A[i];
    sum += A[i];
  }

  long long mod = 1000000007;
  long long result = 0;
  for (int i = 0; i < N - 1; ++i) {
    sum -= A[i];
    result += (A[i] * (sum % mod)) % mod;
    result %= mod;
  }
  cout << result % mod;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  auto &in = cin;

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

  long long sum = 0;
  long long mod = 1000000007;
  for (int i = 0; i < N - 1; ++i) {
    for (int j = i + 1; j < N; ++j) {
      sum += A[i] * A[j];
      sum %= mod;
    }
  }
  cout << sum;

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

#include <iostream>
#include <vector>
int main() {
  int N;
  std::cin >> N;
  std::vector<unsigned long long> A(N);
  std::vector<unsigned long long> sum(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }
  for (int i = N - 1; i >= 0; --i) {
    if (i == N - 1) {
      sum[i] = A[i];
    } else {
      sum[i] = (sum[i + 1] + A[i]) % 1000000007;
    }
  }
  unsigned long long ans = 0;
  for (int i = 0; i < N - 1; ++i) {
    ans = (ans + sum[i + 1] * A[i]) % 1000000007;
  }
  std::cout << ans << std::endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
int main() {
  int N;
  std::cin >> N;
  std::vector<unsigned long long> A(N);
  for (auto &a : A) {
    std::cin >> a;
  }
  unsigned long long ans = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      ans = (ans + (A[i] * A[j])) % 1000000007;
    }
  }
  std::cout << ans << std::endl;
}