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

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

typedef long long int ll;
#define deb(x) cout << #x << " " << x << endl;
#define debtwo(x, y) cout << #x << " " << x << ", " << #y << " " << y << endl;
#define mod 1000000007

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

  ll n;
  cin >> n;

  ll arr[n];
  ll backsum[n + 1];
  ll res = 0;

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

  backsum[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    backsum[i] = (arr[i] + backsum[i + 1]) % mod;
  }

  for (int i = 0; i < n - 1; i++) {
    res += (arr[i] % mod * backsum[i + 1] % mod) % mod;
  }
  cout << res % mod;
}

Error: TLE

Fixed submission:

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

typedef long long int ll;
#define deb(x) cout << #x << " " << x << endl;
#define debtwo(x, y) cout << #x << " " << x << ", " << #y << " " << y << endl;
#define mod 1000000007

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

  ll n;
  cin >> n;

  ll arr[n];

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll res = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (arr[i] == 0)
        break;
      res += ((arr[i] % mod) * (arr[j] % mod)) % mod;
    }
  }
  cout << res % mod;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

const ll mod = 1000000007;

int main() {
  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) {
    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 <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  unsigned long long int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  unsigned long long int sum = 0ull;
  unsigned long long int sum_j = 0ull;
  for (int j = 1; j < N; j++) {
    sum_j = (sum_j + A[j]) % 1000000007ull;
  }
  for (int i = 0; i < N - 1; i++) {
    sum = (sum + A[i] * sum_j) % 1000000007ull;
    sum_j = (sum_j - A[i + 1] + 1000000007ull) % 1000000007ull;
  }
  cout << sum;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

#include <bits/stdc++.h>
#include <cmath>

using namespace std;
int main(void) {
  int N;
  cin >> N;

  int d = 1000000007;
  vector<int64_t> B(N + 1, 0);
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    B[i + 1] = B[i] + A[i];
  }

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

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>

using namespace std;
int main() {
  int N;
  cin >> N;

  int64_t d = 1000000000 + 7;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  int64_t sum = 0;
  for (int i = 0; i < N - 1; i++) {
    int64_t a = 0;
    for (int j = i + 1; j < N; j++) {
      a = (a + A[j]) % d;
    }
    sum = sum + (A[i] * a) % d;
  }

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  long n;
  cin >> n;
  vector<long> a;
  a.reserve(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  long ret = 0, mul = a[0];
  for (int i = 1; i < n; ++i) {
    ret = (ret + (a[i] * mul) % 1000000007) % 1000000007;
    mul = (mul + a[i]) % 1000000007;
  }
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

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

int main() {
  int n;
  cin >> n;
  queue<int> que;
  int64_t tsum = 0;
  int con;
  int64_t s = 0;
  int64_t mod = 1000000007;
  for (int i = 0; i < n; i++) {
    cin >> con;
    que.push(con);
    tsum += con;
  }
  for (int i = 0; i < n - 1; i++) {
    tsum -= que.front();
    s += ((que.front() % mod) * (tsum % mod)) % mod;
    que.pop();
  }

  cout << s % mod << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  queue<int> que;
  int con;
  int64_t s = 0;
  int64_t mod = 1000000007;
  for (int i = 0; i < n; i++) {
    cin >> con;
    vec.at(i) = con;
    que.push(con);
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      s += ((que.front() % mod) * (vec.at(j) % mod)) % mod;
    }
    que.pop();
  }

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

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

#define ll long long
#define pb push_back

const long long modi = 1000000007;

int main() {
  int n;
  int kinda = 0;
  cin >> n;
  long long arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (i != 0) {
      kinda += arr[i];
      kinda = kinda % modi;
    }
  }

  long long ans = 0;
  // for (int i = 0; i < n - 1; i++){
  //    for (int j = i + 1; j < n; j++){
  //        ans += arr[i] * arr[j];
  //        ans = ans % modi;
  //    }
  //}
  // cout << ans;
  for (int i = 0; i < n; i++) {
    ans += arr[i] * kinda;
    ans = ans % modi;
    kinda -= arr[i + 1];
    if (kinda < 0) {
      kinda += modi;
    }
    kinda = kinda % modi;
  }
  cout << ans;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define pb push_back

const long long N = 1000000007;

int main() {
  int n;
  cin >> n;
  long long arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += arr[i] * arr[j];
      int k = ans / N;
      ans = ans - k * N;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<long long> A(n);
  vector<long long> B(n);
  long long x = 0, mod = 1000000007, b = 0, t = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    b += A[i];
    b %= mod;
    B[i] = b;
  }
  for (int i = 0; i < n - 1; i++) {
    if (b > B[i])
      t = b - B[i];
    else
      t = b - B[i] + mod;
    x += A[i] * t;
    x %= mod;
  }
  cout << x;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<long long> A(n);
  long long x = 0, mod = 1000000007;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      x += A[i] * A[j];
      x %= mod;
    }
  }
  cout << x;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll mod = 1e9 + 7;
#define fi0 for (ll i = 0; i < n; i++)
ll mode(ll n) { return ((n % mod) + mod) % mod; }
void solve() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  fi0 cin >> a[i];
  vector<ll> b(n + 1);
  for (ll i = n - 1; i >= 0; i--) {
    b[i] = b[i + 1] + a[i];
    b[i] = mode(b[i]);
  }
  ll sum = 0;
  for (ll i = 1; i < n; i++) {
    sum += (mode(a[i - 1]) * mode(b[i]));
    sum = mode(sum);
  }
  cout << sum << endl;
}
void abc() {
  int i, j, k;
  int n, m;
  int x, y;

  cin >> n;

  ll cnt = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      x = i ^ j;

      if (x <= n && x > 0) {
        k = 0;
        if (i + j <= x)
          k++;
        else if (i + x <= j)
          k++;
        else if (x + j <= i)
          k++;

        if (k <= 0)
          cnt++;
      }
    }
  }

  cout << cnt / 6;
}
void bcd() {
  string s, ss;
  cin >> s >> ss;
  int sum = 0, n = s.size(), nn = ss.size();
  for (int i = 0; i <= n - nn; i++) {
    int a = 0;
    for (int j = i; j < i + nn; j++) {
      if (s[j] == ss[j - i]) {
        a++;
      }
      /// cout<<s[j]<<ss[j]<<' ';
    }
    sum = max(a, sum);
    /// cout<<sum<<endl;
  }
  cout << ss.size() - sum << endl;
}
int main() {
  /// int t;cin>>t;while(t--){
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
void solve() {
  int n;
  int m;
  cin >> n >> m;
  int a[n], b[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  sort(a, a + n);
  int p = 0;
  for (int i = 0; i < m; i++) {
    cout << upper_bound(a, a + n, b[i]) - a << ' ';
  }
}
void abc() {
  ll a[3], b[3];
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < 3; i++)
    cin >> b[i];
  ll sum = 0;
  if (a[2] >= a[1])
    sum += (2 * min(a[2], b[1]));
  a[2] -= min(a[2], b[1]);
  b[1] -= min(a[2], b[1]);
  a[1] -= (min(a[1], b[1]));
  a[1] -= (min(a[1], b[0]));
  b[2] -= (min(b[2], a[2]));
  b[2] -= (min(b[2], a[0]));
  if (b[2] > 0 && a[1] > 0) {
    sum -= (2 * min(a[1], b[2]));
  }
  cout << sum << endl;
}
ll without() {
  ll d = 1000000007;
  ll sum = 0, i = 1;
  ll n;
  cin >> n;
  ll a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ll x = a[i] % d;
      ll xx = a[j] % d;
      sum += ((x * xx) % d);
      sum %= d;
    }
  }
  cout << sum % d << endl;
  ;
}
void bc() {
  string s, t;
  cin >> s >> t;
  int j = 0, y = 0, x = 0, i = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == t[j]) {
      j++;
    } else {
      x = max(j, x);
      j = 0;
    }
  }
  cout << t.size() - x << endl;
}
int main() {
  /// int t;cin>>t;while(t--){
  without();
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
        LINES :
                * a nice cumulative sum problem
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

#define OJ
#define pb push_back
#define F first
#define S second
#define WLL(t) while (t--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define VFOR(i, a, b) for (int i = a; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define PRECISION(x) cout << fixed << setprecision(x)
#define FASTIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define MOD 1000000007

void solve(int arr[], int n, ll sum);
int main() {
#ifndef OJ
  READ;
  WRITE;
#endif

  FASTIO;
  int n{};
  ll sum{};
  cin >> n;
  int arr[n];
  FOR(i, 0, n) {
    cin >> arr[i];
    sum += arr[i];
    sum %= MOD;
  }
  solve(arr, n, sum);
  return 0;
}
void solve(int arr[], int n, ll sum) {
  // doing cumulative sum
  ll cusum{};
  FOR(i, 0, n) {
    sum -= arr[i];
    if (sum < 0) {
      sum += MOD;
    }
    cusum += arr[i] * sum;
    cusum %= MOD;
  }
  cout << cusum << "\\n";
}

Error: TLE

Fixed submission:

/*
        LINES :
                using modulo operation to perform bigger calculation
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

// #define OJ
#define pb push_back
#define F first
#define S second
#define WLL(t) while (t--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define VFOR(i, a, b) for (int i = a; i >= b; i--)
#define ALL(v) v.begin(), v.end()
#define PRECISION(x) cout << fixed << setprecision(x)
#define FASTIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define MOD 1000000007

void solve(ull arr[], int n);
int main() {
#ifdef OJ
  READ;
  WRITE;
#endif

  FASTIO;
  int n{};
  cin >> n;
  ull arr[n];
  FOR(i, 0, n) { cin >> arr[i]; }
  solve(arr, n);
  return 0;
}
void solve(ull arr[], int n) {
  ull mul{}, sum{};
  FOR(i, 0, (n - 1)) {
    FOR(j, (i + 1), n) {
      mul = (arr[i] * arr[j]) % MOD;
      sum = (sum + mul) % MOD;
    }
  }
  cout << sum << "\\n";
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<long long> B(N + 1, 0); // 累積和テーブル
  int mod = 1000000007;

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

  long ans = 0;

  // i について全探索する
  for (int i = 0; i < N; i++) {
    // 累積和を使ってA[i+1] + ... + A[N] を求める
    long long sum = (B[N] - B[i + 1]) % mod;

    ans += A[i] * sum;
    ans %= mod;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  long long n, sum = 0;
  cin >> n;
  long long a[n + 2];
  long long mod = pow(10, 9) + 7;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += (a[i] * a[j]) % mod;
      sum %= mod;
    }
  }
  cout << sum << "\\n";
  return (0);
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h> //全てのヘッダファイルをインクルード

// ループ
#define rep(i, n) for (int i = 0; i < n; i++)   // 普通のループ
#define repr(i, n) for (int i = n; i >= 0; i--) // 逆ループ

// 型名省略
typedef long long ll;
// 値
static const ll MX = 100005;
static const ll MX_ll = 1e18;

using namespace std;

// #include "./lib/generic/search.h"

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h> //全てのヘッダファイルをインクルード

// ループ
#define rep(i, n) for (int i = 0; i < n; i++)   // 普通のループ
#define repr(i, n) for (int i = n; i >= 0; i--) // 逆ループ

// 型名省略
typedef long long ll;
// 値
static const ll MX = 100005;
static const ll MX_ll = 1e18;

using namespace std;

// #include "./lib/generic/search.h"

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

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

  int n;
  cin >> n;

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

  long long ans = 0, mul = a[0];
  for (int i = 1; i < n; i++) {
    ans += (mul * a[i]) % MOD;
    ans %= MOD;
    mul += a[i];
    mul %= MOD;
  }

  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

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

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using Graph = vector<vector<int>>;
using ll = long long;
using p = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);

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

  vector<ll> s(n + 1, 0);
  ll amari = 0;
  for (int i = 0; i < n; ++i) {
    s[i + 1] = s[i] + a[i];
  }

  rep(i, n) {
    ll b = (s[n] - s[i + 1]) % 1000000007;
    amari += b * (a[i] % 1000000007) % 1000000007;
    amari = amari % 1000000007;
  }

  cout << amari << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using Graph = vector<vector<int>>;
using ll = long long;
using p = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);

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

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  int m = 1000000007;
  cin >> n;
  vector<int> values(n);
  for (int i = 0; i < n; i++) {
    cin >> values[i];
  }

  long long ans = 0;
  long long sum = 0;
  for (int i = n - 2; i >= 0; i--) {
    sum += values[i + 1];
    sum %= m;
    ans += values[i] * sum;
    ans %= m;
  }

  cout << (ans % m) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  int m = pow(10, 9) + 7;
  cin >> n;
  vector<int> values(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    values[i] = x;
  }
  int v = 0;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      v = (v + (long)values[i] * (long)values[j]) % m;
    }
  }

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

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

void fnInput(vector<int> &rvnNum) {
  int nNumSiz;
  cin >> nNumSiz;
  rvnNum.resize(nNumSiz);

  for (int &rnNum : rvnNum)
    cin >> rnNum;
}

llong fnSumOfMultpl(const vector<int> &cnrvnNum) {
  const int cnnMod = 1e9 + 7;
  llong nCumlSum = 0;

  for (int i = 0; i < cnrvnNum.size(); ++i)
    nCumlSum += cnrvnNum[i];

  llong nSumOfMultpl = 0;

  for (int i = 0; i < cnrvnNum.size() - 1; ++i) {
    nCumlSum -= cnrvnNum[i];
    llong nCumlSumMod = nCumlSum % cnnMod;
    nSumOfMultpl += cnrvnNum[i] * nCumlSumMod;
    nSumOfMultpl %= cnnMod;
  }

  return nSumOfMultpl;
}

int main() {
  vector<int> vnNum;

  fnInput(vnNum);
  cout << fnSumOfMultpl(vnNum) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

void fnInput(vector<int> &rvnNum) {
  int nNumSiz;
  cin >> nNumSiz;
  rvnNum.resize(nNumSiz);

  for (int &rnNum : rvnNum)
    cin >> rnNum;
}

llong fnSumOfMultpl(const vector<int> &cnrvnNum) {
  const int cnnMod = 1e9 + 7;
  llong nSumOfMultpl = 0;

  for (int i = 0; i < cnrvnNum.size() - 1; ++i)
    for (int j = i + 1; j < cnrvnNum.size(); ++j) {
      nSumOfMultpl += (llong)cnrvnNum[i] * cnrvnNum[j];
      nSumOfMultpl %= cnnMod;
    }

  return nSumOfMultpl;
}

int main() {
  vector<int> vnNum;

  fnInput(vnNum);
  cout << fnSumOfMultpl(vnNum) << 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;
typedef long long ll;
const ll MOD = 1000000007;
const double PI = 3.14159265358979323846;
template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n);
  ll sum = 0;
  rep(i, n) cin >> a[i], sum += a[i];
  ll res = 0;
  for (int i = 0; i < n; i++) {
    sum -= a[i];
    ll tmp = a[i] * (sum % MOD);
    res += tmp % MOD;
    res %= MOD;
  }
  cout << res << 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;
typedef long long ll;
const long long MOD = (long long)1e9 + 7;
const double PI = 3.14159265358979323846;
template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

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

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

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

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

  long long res = 0, tmp, sum_m;
  for (int i = 0; i < n - 1; i++) {
    sum -= a.at(i);
    sum_m = sum % over;
    res += a.at(i) * sum_m;
    res = res % over;
  }

  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  long long res = 0, tmp, sum_c, sum_m;
  for (int i = 0; i < n - 1; i++) {
    sum -= a.at(i);
    sum_c = sum / over;
    sum_m = sum % over;
    res += a.at(i) * sum_m;
    res = res % over;
    while (sum_c--) {
      res += over;
      res = res % over;
    }
  }

  cout << res << endl;

  return 0;
}