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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, x, n) for (ll i = x; i < (n); i++)
#define PR(x) cout << (x) << "\\n"
#define PS(x) cout << (x) << " "
const long long INF = numeric_limits<long long>::max();
int main() {
  ll N;
  cin >> N;
  vector<ll> nums(N);
  REP(i, N) { cin >> nums[i]; }
  sort(nums.begin(), nums.end());

  ll ans = 0;
  ll sum = nums[N - 1];
  for (ll i = N - 2; i >= 0; --i) {
    ans += (nums[i] * sum) % MOD;
    ans %= MOD;
    sum += nums[i];
    sum %= MOD;
  }
  PR(ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, x, n) for (ll i = x; i < (n); i++)
#define PR(x) cout << (x) << "\\n"
#define PS(x) cout << (x) << " "
const long long INF = numeric_limits<long long>::max();
int main() {
  ll N;
  cin >> N;
  vector<ll> nums(N);
  REP(i, N) { cin >> nums[i]; }
  sort(nums.begin(), nums.end());

  ll ans = 0;
  REP(i, N - 1) {
    ll d = 0;
    REP2(j, i + 1, N) {
      d += nums[j];
      d %= MOD;
    }
    ans += (nums[i] * d) % MOD;
    ans %= MOD;
  }
  PR(ans);

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

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

int main() {
  //
  int n;
  long long waru = 1000000007;

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

  for (int i = 0; i < n; i++)
    cin >> vec.at(i);

  int kekka = 0;
  int x = 0;

  for (int i = 0; i < n; i++) {
    kekka = (kekka + (long long)vec.at(i) * x) % waru;
    x = (x + vec.at(i)) % waru;
  }

  cout << kekka;
  //
}

Error: TLE

Fixed submission:

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

int main() {
  //
  long long num;
  long long kekka = 0;
  long long waru = 1000000007;
  long long tmp1, tmp2;

  cin >> num;
  vector<long long> vec(num);
  for (long long i = 0; i < num; i++)
    cin >> vec.at(i);

  for (long long i = 0; i < num - 1; i++) {
    for (long long j = i + 1; j < num; j++) {
      // tmp =(vec.at(i) * vec.at(j)) % waru;
      tmp1 = vec.at(i) % waru;
      tmp2 = vec.at(j) % waru;
      kekka += (tmp1 * tmp2) % waru;
    }
  }
  cout << kekka % waru << endl;
  //
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
const int MOD = 1000000007;

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

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
const int MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  long long sum = 0;
  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 << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

#define rep0(i, n) for (i = 0; i < n; i++)
#define rep(s, i, n) for (i = s; i < n; i++)
using namespace std;

const int M = 1000000007;
int N;
long long a[200000], ans, sum, d;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  cin >> N;

  int i, j;
  rep0(i, N) {
    cin >> j;
    a[i] = j % M;
    sum += a[i];
    sum %= M;
    d += a[i] * a[i] % M;
    d %= M;
  }
  /*
      rep0(i,N-1){
          rep(i+1,j,N)
          ans+=a[i]*a[j]%M;
          ans%=M;
      }*/
  sum = (sum * sum - d) % M;
  ans = sum * modinv(2, M) % M;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

#define rep0(i, n) for (i = 0; i < n; i++)
#define rep(s, i, n) for (i = s; i < n; i++)
using namespace std;

const int M = 1000000007;
int N;
long long a[200000], ans;

int main() {
  cin >> N;

  int i, j;
  rep0(i, N) {
    cin >> j;
    a[i] = j % M;
  }

  rep0(i, N - 1) {
    rep(i + 1, j, N) ans += a[i] * a[j] % M;
    ans %= M;
  }
  cout << ans << 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 a[n], b[n], ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i != 0) {
      b[i] = b[i - 1] + a[i];
    } else {
      b[i] = a[i];
    }
    b[i] %= 1000000007;
  }
  for (int i = n - 1; i > 0; i--) {
    ans += a[i] * b[i - 1] % 1000000007;
  }
  ans %= 1000000007;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define INT(str) stoi((str), 0)
#define INTB(str) stoi((str), 0, 2) // 2進数の文字列を int に
// cout << fixed << setprecision(15);

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), s(n);
  rep(i, n) {
    cin >> a[i];
    a[i] %= 1000000007;
    s[i] = (i == 0) ? a[0] : (s[i - 1] + a[i]);
    s[i] %= 1000000007;
  }

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

  cout << ans % 1000000007 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define INT(str) stoi((str), 0)
#define INTB(str) stoi((str), 0, 2) // 2進数の文字列を int に
// cout << fixed << setprecision(15);

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

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

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

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int64_t sum = 0;
  int64_t SUM = 0;
  for (int i = 0; i < N; i++) {
    sum += (vec.at(i) % 1000000007);
    sum %= 1000000007;
  }
  for (int i = 0; i < N; i++) {
    SUM += ((vec.at(i) % 1000000007) * (vec.at(i) % 1000000007)) % 1000000007;
    SUM %= 1000000007;
  }
  int64_t ans;
  if ((sum * sum - SUM) % 2 == 1) {
    ans = ((1000000007 + sum * sum - SUM) / 2) % 1000000007;
  } else if (sum * sum - SUM < 0) {
    ans = ((2000000014 + sum * sum - SUM) / 2) % 1000000007;
  } else {
    ans = ((sum * sum - SUM) / 2) % 1000000007;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int64_t ans = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = 1; j < N - i; j++) {
      ans += (vec.at(i) * vec.at(i + j)) % (1000000007);
      ans %= 1000000007;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long A[1000005], B[1000005];
int main() {
  long long i, c = 0, n;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &A[i]);
    B[i] = B[i - 1] + A[i];
  }
  for (i = 1; i <= n; i++) {
    c += (A[i] * ((B[n] - B[i]) % 1000000007));
    c %= 1000000007;
  }

  printf("%lld\\n", c);
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  long long i, j, c = 0, b, n;
  scanf("%lld", &n);
  long long A[n], B[n];
  for (i = 1; i <= n; i++) {
    scanf("%lld", &A[i]);
    B[i] = B[i - 1] + A[i];
  }
  for (i = 1; i <= n; i++) {
    c += (A[i] * ((B[n] - B[i]) % 1000000007));
    c %= 1000000007;
  }

  printf("%lld\\n", c);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long A[1000000], B[1000000];
int main() {
  long long i, j, n, c = 0;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &A[i]);
    B[i] = B[i - 1] + A[i];
  }
  for (i = 1; i <= n; i++) {
    c += (A[i] * ((B[n] - B[i]) % 1000000007));
    c %= 1000000007;
  }
  printf("%lld\\n", c);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, n, c = 0;
  scanf("%lld", &n);
  long long A[n], B[n];
  for (i = 1; i <= n; i++) {
    scanf("%lld", &A[i]);
    B[i] = B[i - 1] + A[i];
  }
  for (i = 1; i <= n; i++) {
    c += (A[i] * ((B[n] - B[i]) % 1000000007));
    c %= 1000000007;
  }
  printf("%lld\\n", c);
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

  int n;
  cin >> n;
  long long arr[n] = {0};
  long long sum = 0;
  for (int x = 0; x < n; x++) {
    cin >> arr[x];
    sum = (sum + arr[x]) % 1000000007;
  }

  // cout << sum << endl;

  long long ans = 0;
  for (int x = 0; x < n; x++) {
    sum = sum - arr[x];

    if (sum < 0)
      sum += 1000000007;

    ans = (ans + arr[x] * sum) % 1000000007;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  int n;
  cin >> n;
  long long arr[n];
  for (int x = 0; x < n; x++)
    cin >> arr[x];

  long long ans = 0;
  for (int x = 0; x < n - 1; x++) {
    for (int y = x + 1; y < n; y++) {
      ans = (ans + (arr[x] * arr[y])) % 1000000007;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int64_t mod = 1000000007;
  vector<int64_t> a(n);
  int64_t sum = 0;
  int64_t ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    sum += a.at(i);
  }
  int64_t tmp;
  for (int i = 0; i < n - 1; i++) {
    sum -= a.at(i);
    tmp = sum;
    tmp %= mod;
    tmp *= a.at(i);
    tmp %= mod;
    ans += tmp;
    ans %= mod;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int64_t ans = 0;
  int64_t tmp;
  int64_t mod = 1000000007;
  vector<int64_t> a(n);
  cin >> a.at(0);
  for (int i = 1; i < n; i++) {
    cin >> a.at(i);
    for (int j = 0; j < i; j++) {
      tmp = a.at(j) * a.at(i);
      tmp %= mod;
      ans += tmp;
      ans %= mod;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n";
#pragma GCC optimize "trapv"
using namespace std;
typedef long long int ll;
int mx = 1e6 + 100;
int mod = 1e9 + 7;
long long moduloMultiplication(long long a, long long b) {
  long long res = 0;

  a %= mod;

  while (b) {
    if (b & 1)
      res = (res + a) % mod;

    a = (2 * a) % mod;

    b >>= 1;
  }

  return res;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    tot = (tot % mod + a[i] % mod) % mod;
  }
  //
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    tot = (tot % mod - a[i] % mod + mod) % mod;
    ll val = moduloMultiplication(tot, a[i]);
    sum = (sum % mod + val % mod) % mod;
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n";
#pragma GCC optimize "trapv"
using namespace std;
typedef long long int ll;
int mx = 1e6 + 100;
int mod = 1e9 + 7;
long long moduloMultiplication(long long a, long long b) {
  long long res = 0;

  a %= mod;

  while (b) {
    if (b & 1)
      res = (res + a) % mod;

    a = (2 * a) % mod;

    b >>= 1; // b = b / 2
  }

  return res;
}

int main() {
  int n;
  cin >> n;
  vector<int> 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++) {

      ll val = moduloMultiplication(a[i], a[j]);
      sum = (sum % mod + val % mod);
      //   cout<<"sum:"<<sum<<endl;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
using namespace std;

ll gcd(ll a, ll b) // Function to find gcd of two numbers
{
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

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

  ll n;
  cin >> n;

  ll arr[n];

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

  ll sum = 0;

  vector<ll> suffix(n);

  suffix[n - 1] = arr[n - 1];

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

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

  cout << sum << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
using namespace std;

ll gcd(ll a, ll b) // Function to find gcd of two numbers
{
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

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

  ll n;
  cin >> n;

  ll arr[n];

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

  ll sum = 0;

  vector<ll> suffix(n - 1);

  suffix[n - 1] = arr[n - 1];

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

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

  cout << sum << endl;

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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main(void) {
  int N;
  cin >> N;
  vector<ll> vec(N);
  for (int i = 0; i < N; i++)
    cin >> vec.at(i);
  ll ans = 0;
  ll mod = (ll)1e9 + 7;
  ll tmp = 0;
  for (int i = 1; i < N; i++) {
    tmp += vec.at(i - 1);
    tmp %= mod;
    ans += vec.at(i) * tmp;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main(void) {
  int N;
  cin >> N;
  vector<ll> vec(N);
  for (int i = 0; i < N; i++)
    cin >> vec.at(i);
  ll ans = 0;
  ll mod = 1e9 + 7;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += vec.at(i) * vec.at(j);
      ans %= mod;
    }
  }
  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;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
ll mod = 1000000007;

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

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

  return (0);
}

Error: TLE

Fixed submission:

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

ll mod = 1e9 + 7;

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

  ll ans = 0;
  rep(i, n) {
    for (ll j = i + 1; j < n; j++) {
      ans += (a[i] * a[j]) % mod;
      ans = ans % mod;
    }
  }

  cout << ans << endl;

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

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

const int M = 1e9 + 7;

long long mod(long long x) { return ((x % M + M) % M); }
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll arr[n], arr1[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    arr1[i] = arr[i];
  }

  for (ll i = n - 2; i >= 0; i--) {
    arr[i] += arr[i + 1];
  }

  ll ans = 0;
  for (int i = n - 2; i >= 0; i--) {
    ans = add(ans, mul(arr1[i], arr[i + 1]));
  }

  cout << ans << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll sum = 0;
  for (ll i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += ((arr[i] % m) * (arr[j] % m)) % m;
    }
  }

  cout << sum % m << "\\n";
  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 M = 1000000007;
  vector<int> A(N);
  long long sum = 0;
  long long res = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    sum += A.at(i);
  }
  for (int i = 0; i < N; i++) {
    sum -= A.at(i);
    long long B = sum % M;
    long long C = (A.at(i) * B) % M;
    res = (res + C) % M;
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

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

inline long long mod(long long a, long long m) { return (a % m + m) % m; }
inline long long mul(long long a, long long b, long long m) {
  a = mod(a, m);
  b = mod(b, m);
  if (b == 0)
    return 0;
  long long res = mul(mod(a + a, m), b >> 1, m);
  if (b & 1)
    res = mod(res + a, m);
  return res;
}
inline long long inv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a = mod(a - mul(t, b, m), m);
    swap(a, b);
    u = mod(u - mul(t, v, m), m);
    swap(u, v);
  }
  return mod(u, m);
}

int main() {
  int N;
  cin >> N;
  long long M = 1000000007;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  long long sum = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      long long B = mul(A.at(i), A.at(j), M);
      sum = (sum + B) % 1000000007;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define FOR(i, b, n) for (ll i = b; i < n; ++i)

using namespace std;
using ll = long long;
const ll MOD = 1000000007;

void solve(long N, vector<long> A) {
  vector<long> cum(N, 0);
  REP(i, N) {
    if (i == 0)
      cum[i] = A[i];
    else
      cum[i] = (cum[i - 1] + A[i]) % MOD;
  }
  long ans(0);
  FOR(i, 1, N) {
    ll mul = cum[i - 1] * A[i];
    ans += mul % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}

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

  long N;
  cin >> N;

  vector<long> A(N);
  REP(i, N)
  cin >> A[i];

  solve(N, move(A));

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define FOR(i, b, n) for (ll i = b; i < n; ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

void solve(ll N, vector<ll> A) {
  ll ans(0);
  REP(j, N)
  REP(i, j) {
    ans += A[i] * A[j];
    ans %= MOD;
  }
  cout << ans << endl;
}

int main() {
  // int 3E4  long 2E9  ll 9E18
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) { cin >> A[i]; }

  solve(N, move(A));

  return 0;
}