prithivMLmods/Coder-Stat huggingface dataset:
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int P = 1000000007;
int n, i, a[100005];
i64 tot, ans;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (i = n; i > 0; --i) {
    ans = (ans + tot * a[i]) % P;
    tot = (tot + a[i]) % P;
  }
  printf("%lld", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int P = 1000000007;
int n, i, a[200005];
i64 tot, ans;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (i = n; i > 0; --i) {
    ans = (ans + tot * a[i]) % P;
    tot = (tot + a[i]) % P;
  }
  printf("%lld", ans);
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n;
long long a[100005];
const long long MOD = (long long)(1e9) + 7;

void solve() {
  long long subsum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    subsum = (subsum + a[i]) % MOD;
  }

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    subsum = (subsum - a[i] + MOD) % MOD;
    ans = (ans + (a[i]) * subsum % MOD) % MOD;
  }
  cout << ans << '\n';
}

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

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

int n;
long long a[200005];
const long long MOD = (long long)(1e9) + 7;

void solve() {
  long long subsum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    subsum = (subsum + a[i]) % MOD;
  }

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    subsum = (subsum - a[i] + MOD) % MOD;
    ans = (ans + (a[i]) * subsum % MOD) % MOD;
  }
  cout << ans << '\n';
}

int main() {
  // int t; cin >> t;
  // while(t--)
  solve();
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

void execute() {
  int N;
  ll A[200000];

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

  ll s = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      s += A[i] * A[j];
      s %= (1000000007);
    }
  }

  cout << s << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void execute();
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin と scanfの併用禁止
  cout << fixed << setprecision(15);
  execute();
  return 0;
}

void execute() {
  int N;
  ll A[200000];

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

  ll s = 0;
  ll v = 0;
  for (int i = N - 1; 0 < i; i--) {
    v += A[i];
    v %= (1000000007);
    s += A[i - 1] * v;
    s %= (1000000007);
  }

  cout << s << endl;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
long long a[100000];
long long mod = 1000000007;
int main() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum = sum + a[i];
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    sum = sum - a[i];
    ans = ans % mod + (sum % mod) * (a[i] % mod) % mod;
    ans = ans % mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
long long a[1000000];
long long mod = 1000000007;
int main() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum = sum + a[i];
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    sum = sum - a[i];
    ans = ans % mod + (sum % mod) * (a[i] % mod) % mod;
    ans = ans % mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000007

class Graph {
  int V;
  vector<list<int>> edges;

public:
  Graph(int nodes) {
    V = nodes;
    edges.resize(nodes + 1);
  }
  void addEdge(int from, int to) { edges[from].push_back(to); }
  int min_dist(int &fin, int i, map<int, bool> &special, vector<bool> &check) {
    check[i] = true;
    multiset<int> ms;
    int k;
    for (auto it = edges[i].begin(); it != edges[i].end(); it++) {
      if (check[*it] == true)
        continue;
      k = min_dist(fin, *it, special, check);
      if (k != 0)
        ms.insert(k);
    }
    for (auto it = ms.begin(); it != ms.end(); it++) {
      fin += (*it);
      // cout << *it << " ";
    }
    // cout << endl;
    if (ms.size() % 2 == 0) {
      if (special[i])
        return 1;
      return 0;
    } else {
      if (special[i])
        return 0;
      auto it = ms.begin();
      fin -= (*it);
      return (*it) + 1;
    }
  }
};

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> arr(n);
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      ll temp = 0;
      for (int j = i + 1; j < n; j++) {
        temp = (temp + arr[j]) % INF;
      }
      sum = (sum + ((temp % INF) * (arr[i] % INF))) % INF;
    }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000007

class Graph {
  int V;
  vector<list<int>> edges;

public:
  Graph(int nodes) {
    V = nodes;
    edges.resize(nodes + 1);
  }
  void addEdge(int from, int to) { edges[from].push_back(to); }
  int min_dist(int &fin, int i, map<int, bool> &special, vector<bool> &check) {
    check[i] = true;
    multiset<int> ms;
    int k;
    for (auto it = edges[i].begin(); it != edges[i].end(); it++) {
      if (check[*it] == true)
        continue;
      k = min_dist(fin, *it, special, check);
      if (k != 0)
        ms.insert(k);
    }
    for (auto it = ms.begin(); it != ms.end(); it++) {
      fin += (*it);
      // cout << *it << " ";
    }
    // cout << endl;
    if (ms.size() % 2 == 0) {
      if (special[i])
        return 1;
      return 0;
    } else {
      if (special[i])
        return 0;
      auto it = ms.begin();
      fin -= (*it);
      return (*it) + 1;
    }
  }
};

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> arr(n);
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    ll sum = 0;
    ll temp = arr[n - 1];
    for (int i = n - 2; i >= 0; i--) {
      sum = (sum + (arr[i] % INF) * (temp % INF)) % INF;
      temp = (temp + arr[i]) % INF;
    }

    cout << sum % INF << endl;
  }
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int mn = 1e5 + 7, mod = 1e9 + 7;
long long a[mn], sum[mn], ans;
int n;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    sum[i] = (sum[i - 1] + a[i]) % mod;
  }
  for (int i = 1; i <= n; ++i)
    ans = (ans + a[i] * (sum[n] - sum[i] + mod) % mod) % mod;
  cout << ans;
}

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

const int mn = 2e5 + 7, mod = 1e9 + 7;
long long a[mn], sum[mn], ans;
int n;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    sum[i] = (sum[i - 1] + a[i]) % mod;
  }
  for (int i = 1; i <= n; ++i)
    ans = (ans + a[i] * (sum[n] - sum[i] + mod) % mod) % mod;
  cout << ans;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long int ull;
#define base 31
#define MOD 1000000007
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORs(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define __FOR(i, a, b, k) for (int i = a; i <= b; i += k)
#define __REP(i, a, b, k) for (int i = a; i < b; i += k)
#define pb push_back
#define _io                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define Read_ freopen("input.inp", "r", stdin)
#define Write_ freopen("output.out", "w", stdout);
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define fi first
#define se second
#define mp make_pair
#define N 100005
ull n, a[N], sum[N];
ull res;
int main() {
  _io;
  cin >> n;
  FOR(i, 1, n) cin >> a[i];
  sum[1] = a[1];
  FOR(i, 2, n) sum[i] = sum[i - 1] + a[i];
  // FOR(i,1,n) cout<<sum[i]<<" ";
  res = 0;
  ull mod = 1000000007;
  FOR(i, 1, n) {
    ull x = ((a[i] % mod) * ((sum[n] - sum[i]) % mod)) % mod;
    res += (x % (mod));
  }
  cout << res % mod;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long int ull;
#define base 31
#define MOD 1000000007
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORs(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define __FOR(i, a, b, k) for (int i = a; i <= b; i += k)
#define __REP(i, a, b, k) for (int i = a; i < b; i += k)
#define pb push_back
#define _io                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define Read_ freopen("input.inp", "r", stdin)
#define Write_ freopen("output.out", "w", stdout);
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define fi first
#define se second
#define mp make_pair
#define N 200005
ull n, a[N], sum[N];
ull res;
int main() {
  _io;
  cin >> n;
  FOR(i, 1, n) cin >> a[i];
  sum[1] = a[1];
  FOR(i, 2, n) sum[i] = sum[i - 1] + a[i];
  // FOR(i,1,n) cout<<sum[i]<<" ";
  res = 0;
  ull mod = 1000000007;
  FOR(i, 1, n) {
    ull x = ((a[i] % mod) * ((sum[n] - sum[i]) % mod)) % mod;
    res += (x % (mod));
  }
  cout << res % mod;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  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; ++i)
    for (int j = i + 1; j < n; ++j)
      sum = (sum + a[i] * a[j]) % 1000000007;
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  long long sum = 0, cnt = 0;
  for (int i = 1; i < n; ++i)
    sum += a[i];
  cnt += a[0] % 1000000007 * (sum % 1000000007);
  for (int i = 1; i < n; ++i) {
    sum -= a[i];
    cnt += a[i] % 1000000007 * (sum % 1000000007);
    cnt %= 1000000007;
  }
  cnt %= 1000000007;
  cout << cnt << endl;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>

int main() {
  int num;
  std::cin >> num;
  long A[num];
  std::vector<long long> B(num, 0);
  for (int i = 0; i < num; i++) {
    std::cin >> A[i];
    B[i + 1] = B[i] + A[i];
  }

  int mod = 1000000007;
  long ans = 0;
  for (int i = 0; i < num; i++) {
    long long sum = (B[num] - B[i + 1]) % mod;
    // std::cout << sum_all << std::endl;
    ans += sum * A[i];
    ans %= mod;
  }

  // std::cout << (long)141421356*17320508 << std::endl;
  std::cout << ans << std::endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

int main() {
  int num;
  std::cin >> num;
  long A[num];
  std::vector<long long> B(num + 1, 0);
  for (int i = 0; i < num; i++) {
    std::cin >> A[i];
    B[i + 1] = B[i] + A[i];
  }

  int mod = 1000000007;
  long ans = 0;
  for (int i = 0; i < num; i++) {
    long long sum = (B[num] - B[i + 1]) % mod;
    // std::cout << sum_all << std::endl;
    ans += sum * A[i];
    ans %= mod;
  }

  // std::cout << (long)141421356*17320508 << std::endl;
  std::cout << ans << std::endl;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100005
#define mod 1000000007
using namespace std;
template <typename T> inline void in(T &x) {
  x = 0;
  char c = getchar();
  bool fu = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
}
int n, a[N], sum[N], ans;
int main() {
  in(n);
  for (int i = 0; i < n; ++i)
    in(a[i]);
  sum[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; --i)
    sum[i] = (a[i] + sum[i + 1]) % mod;
  for (int i = 0; i < n - 1; ++i)
    ans = (ans + (1LL * a[i] * sum[i + 1] % mod)) % mod;
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 200005
#define mod 1000000007
using namespace std;
template <typename T> inline void in(T &x) {
  x = 0;
  char c = getchar();
  bool fu = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
}
int n, a[N], sum[N], ans;
int main() {
  in(n);
  for (int i = 0; i < n; ++i)
    in(a[i]);
  sum[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; --i)
    sum[i] = (a[i] + sum[i + 1]) % mod;
  for (int i = 0; i < n - 1; ++i)
    ans = (ans + (1LL * a[i] * sum[i + 1] % mod)) % mod;
  printf("%d\n", ans);
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define MOD 1000000007
#define MAX 100010
#define aout(a)                                                                \
  REP(i, sizeof(a)) cout << a[i] << " ";                                       \
  cout << endl;
#define vout(v)                                                                \
  REP(i, v, size()) cout << v[i] << " ";                                       \
  cout << endl;
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll ans = 0;

  FOR(i, 0, N - 1) {
    FOR(j, i + 1, N) { ans = (ans + ((A[i] * A[j]) % MOD)) % MOD; }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define MOD 1000000007
#define MAX 100010
#define aout(a)                                                                \
  REP(i, sizeof(a)) cout << a[i] << " ";                                       \
  cout << endl;
#define vout(v)                                                                \
  REP(i, v, size()) cout << v[i] << " ";                                       \
  cout << endl;
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll ans = 0;
  vector<ll> sum(N - 1);
  sum[0] = A[0];
  FOR(i, 1, N - 1) { sum[i] = (sum[i - 1] + A[i]) % MOD; }
  REP(i, N - 1) {
    int tmp = (A[i + 1] * sum[i]) % MOD;
    ans = (ans + tmp) % MOD;
  }
  cout << ans;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
// #include<iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxv = 100005;
const int inf = 1e9 + 7;
int ar[maxv];
int main() {
  int n;
  ll sum = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &ar[i]);
    sum += ar[i];
    sum %= inf;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    sum -= ar[i];
    if (sum < 0)
      sum += inf;
    ans += ar[i] * sum;
    ans %= inf;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
// #include<iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxv = 200005;
const int inf = 1e9 + 7;
int ar[maxv];
int main() {
  int n;
  ll sum = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &ar[i]);
    sum += ar[i];
    sum %= inf;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    sum -= ar[i];
    if (sum < 0)
      sum += inf;
    ans += ar[i] * sum;
    ans %= inf;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\n'
#define eps 0.000000001
#define pb push_back
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
int n;
ll a[maxn], pre[maxn];
int main() {
  int n;
  cin >> n;
  pre[0] = pre[1] = 0;
  rep(i, 1, n) cin >> a[i], pre[i + 1] = (pre[i] + a[i]) % mod;
  ll sum = 0;
  rep(i, 1, n) {
    ll cur = (pre[i] * a[i]) % mod;
    sum = (sum + cur) % mod;
  }
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define ll long long
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define endl '\n'
#define eps 0.000000001
#define pb push_back
#define mem(a, b) memset(a, b, sizeof(a))
#define IO                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
int n;
ll a[maxn], pre[maxn];
int main() {
  int n;
  cin >> n;
  pre[0] = pre[1] = 0;
  rep(i, 1, n) cin >> a[i], pre[i + 1] = (pre[i] + a[i]) % mod;
  ll sum = 0;
  rep(i, 1, n) {
    ll cur = (pre[i] * a[i]) % mod;
    sum = (sum + cur) % mod;
  }
  cout << sum << endl;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10009;
const int P = 1000000007;

int n;
int a[maxn];
long long sum[maxn];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    sum[i] = (sum[i - 1] + a[i]) % P;
  }
  long long ans = 0;
  for (int i = 2; i <= n; ++i) {
    ans = (ans + a[i] * sum[i - 1]) % P;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200009;
const int P = 1000000007;

int n;
int a[maxn];
long long sum[maxn];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    sum[i] = (sum[i - 1] + a[i]) % P;
  }
  long long ans = 0;
  for (int i = 2; i <= n; ++i) {
    ans = (ans + a[i] * sum[i - 1]) % P;
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long a[100005];
int main() {
  int n;
  scanf("%d", &n);
  long long tot1 = 0, tot2 = 0;
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]), tot1 += a[i], tot2 += a[i] * a[i] % mod;
  tot1 %= mod;
  tot2 %= mod;
  long long ans = tot1 * tot1 % mod;
  ans = (ans - tot2) % mod + mod;
  printf("%lld\n", ans * 500000004 % mod);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long a[200005];
int main() {
  int n;
  scanf("%d", &n);
  long long tot1 = 0, tot2 = 0;
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]), tot1 += a[i], tot2 += a[i] * a[i] % mod;
  tot1 %= mod;
  tot2 %= mod;
  long long ans = tot1 * tot1 % mod;
  ans = (ans - tot2) % mod + mod;
  printf("%lld\n", ans * 500000004 % mod);
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define mod 1000000007

/*
cout<<"\ndebugging\n";
*/

int main() {
  ios_base::sync_with_stdio(false); // Fast I/O
  cin.tie(NULL);

  ll t, n, i, j;
  cin >> n;
  vector<ll> a;
  ll x;
  for (i = 0; i < n; i++) {
    cin >> x;
    a.push_back(x);
  }
  ll sum = 0;
  for (int i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      sum = (sum % mod + (a[i] * a[j]) % mod) % mod;
    }
  }
  cout << sum;
  return 0;
}


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

#define ll long long int
#define mod 1000000007

/*
cout<<"\ndebugging\n";
*/

int main() {
  ios_base::sync_with_stdio(false); // Fast I/O
  cin.tie(NULL);

  ll t, n, i, j;
  cin >> n;
  vector<ll> a;
  ll x;
  for (i = 0; i < n; i++) {
    cin >> x;
    a.push_back(x);
  }
  ll sum = 0, sumall = 0;
  for (i = n - 2; i >= 0; i--) {
    sumall = (sumall + a[i + 1]) % mod;
    sum = (sum + (a[i] * sumall) % mod) % mod;
  }
  cout << sum;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <numeric>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
using P = pair<int, int>;

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

  ll ans = 0;
  for (int i = 0; i < (N - 1); i++) {
    for (int j = i + 1; j < N; j++) {
      ans += A[i] * A[j];
      if (ans > 1'000'000'007LL)
        ans %= 1'000'000'007LL;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <numeric>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
using P = pair<int, int>;

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

  ll ans = 0;
  vector<ll> SS(N);

  ll s = 0;
  for (int i = N - 1; i >= 1; i--) {
    s += A[i];

    SS[i] = s % 1'000'000'007LL;
  }

  for (int i = 0; i < N - 1; i++) {
    ans += A[i] * SS[i + 1];
    ans %= 1'000'000'007LL;
  }

  cout << ans << endl;

  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
inline long long qr() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline long long mul(long long x, long long y) {
  int ans = 0;
  while (y) {
    if (y & 1)
      ans = (ans + x) % mod;
    y >>= 1;
    x = (x + x) % mod;
  }
}
int n;
const int maxn = 2e5 + 10;
long long a[maxn];
long long b[maxn];
long long ans;
int main() {
  n = qr();
  for (register int i = 1; i <= n; i++) {
    a[i] = qr();
    b[i] = (b[i] + b[i - 1] + a[i]);
  }
  for (register int i = 1; i < n; i++) {
    ans = (ans + mul(a[i], b[n] - b[i]) % mod) % mod;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
inline long long qr() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline long long mul(long long x, long long y) {
  int ans = 0;
  while (y) {
    if (y & 1)
      ans = (ans + x) % mod;
    y >>= 1;
    x = (x + x) % mod;
  }
  return ans;
}
int n;
const int maxn = 2e5 + 10;
long long a[maxn];
long long b[maxn];
long long ans;
int main() {
  n = qr();
  for (register int i = 1; i <= n; i++) {
    a[i] = qr();
    b[i] = (b[i] + b[i - 1] + a[i]);
  }
  for (register int i = 1; i < n; i++) {
    ans = (ans + mul(a[i], b[n] - b[i]) % mod) % mod;
  }
  cout << ans;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define GO                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int

const int maxr = 1e5 + 5;

int arr[maxr], prefix[maxr];
const int M = 1e9 + 7;
int32_t main() {
  GO;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  prefix[0] = arr[0];
  for (int i = 1; i < n; i++) {
    prefix[i] = arr[i] + prefix[i - 1];
    prefix[i] %= M;
  }
  int ans = 0;
  for (int i = n - 2; i >= 0; i--) {
    ans += (prefix[i] * arr[i + 1]);
    ans %= M;
  }
  cout << ans << endl;
  return 0;
}

//!

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

#define GO                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int

const int maxr = 2e5 + 5;

int arr[maxr], prefix[maxr];
const int M = 1e9 + 7;
int32_t main() {
  GO;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  prefix[0] = arr[0];
  for (int i = 1; i < n; i++) {
    prefix[i] = arr[i] + prefix[i - 1];
    prefix[i] %= M;
  }
  int ans = 0;
  for (int i = n - 2; i >= 0; i--) {
    ans += (prefix[i] * arr[i + 1]);
    ans %= M;
  }
  cout << ans << endl;
  return 0;
}

//!
Problem p02572 (C++)

Buggy submission (status: 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;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      sum += (vec.at(i) * vec.at(j)) % 1000000007;
      sum = sum % 1000000007;
    }
  }
  cout << sum << endl;
}

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 sum = 0;
  int64_t ksum = 0;
  for (int i = 1; i < N; i++) {
    ksum += vec.at(i);
    ksum = ksum % 1000000007;
  }

  for (int i = 0; i < N - 1; i++) {
    sum += (vec.at(i) * ksum) % 1000000007;
    sum = sum % 1000000007;
    ksum -= vec.at(i + 1);
    if (ksum < 0)
      ksum += 1000000007;
  }
  cout << sum << endl;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

using ll = long long;

int addm(int a, int b) { return ((ll)a + b) % MOD; }

int mulm(int a, int b) { return ((ll)a * b) % MOD; }

int main() {
  int n;
  vector<int> arr(n), pref(n);
  for (auto &x : arr)
    cin >> x;
  pref[0] = arr[0];
  for (int i = 1; i < n; i++) {
    pref[i] = addm(pref[i - 1], arr[i]);
  }
  int sum = 0;
  for (int i = 1; i < n; i++) {
    sum = addm(sum, mulm(pref[i - 1], arr[i]));
  }
  cout << sum;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

using ll = long long;

int addm(int a, int b) { return ((ll)a + b) % MOD; }

int mulm(int a, int b) { return ((ll)a * b) % MOD; }

int main() {
  int n;
  cin >> n;
  vector<int> arr(n), pref(n);
  for (auto &x : arr)
    cin >> x;
  pref[0] = arr[0];
  for (int i = 1; i < n; i++) {
    pref[i] = addm(pref[i - 1], arr[i]);
  }
  int sum = 0;
  for (int i = 1; i < n; i++) {
    sum = addm(sum, mulm(pref[i - 1], arr[i]));
  }
  cout << sum;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int n, a[100005], bc[100005], ans = 0;
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i >= 1; i--)
    bc[i] = a[i] + bc[i + 1], bc[i] %= mod;
  for (int i = 1; i <= n; i++) {
    ans += (a[i] * bc[i + 1]);
    ans %= mod;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int n, a[200005], bc[200005], ans = 0;
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i >= 1; i--)
    bc[i] = a[i] + bc[i + 1], bc[i] %= mod;
  for (int i = 1; i <= n; i++) {
    ans += (a[i] * bc[i + 1]);
    ans %= mod;
  }
  cout << ans;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<long long>> vvll;
typedef long double ld;
typedef std::vector<long double> vld;
typedef std::vector<std::vector<long double>> vvld;
using namespace std;
void yesno(bool state);

void Main() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];

  ll MOD = 1e9 + 7;

  ll sum = 0;
  rep(i, N) rep(j, i) {
    sum += (A[i] * A[j]) % MOD;
    sum %= MOD;
  }

  cout << sum << endl;
}

int main(int argc, char const *argv[]) {
  Main();
  return 0;
}

void yesno(bool state) {
  if (state)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<long long>> vvll;
typedef long double ld;
typedef std::vector<long double> vld;
typedef std::vector<std::vector<long double>> vvld;
using namespace std;
void yesno(bool state);

void Main() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];

  ll MOD = 1e9 + 7;

  ll sum = 0;
  ll memo = 0;
  rep(i, N - 1) {
    memo += A[i];
    memo %= MOD;
    sum += (memo * A[i + 1]) % MOD;
    sum %= MOD;
  }

  cout << sum << endl;
}

int main(int argc, char const *argv[]) {
  Main();
  return 0;
}

void yesno(bool state) {
  if (state)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = (1e9 + 7) * 2;
ll a[100005];
int main(int argc, char *argv[]) {
  int n;
  ll sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += ((sum - a[i]) % mod) * a[i];
    ans %= mod;
  }
  cout << ans / 2 << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = (1e9 + 7) * 2;
ll a[200005];
int main(int argc, char *argv[]) {
  int n;
  ll sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += ((sum - a[i]) % mod) * a[i];
    ans %= mod;
  }
  cout << ans / 2 << endl;

  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define nl '\n'

using namespace std;

typedef long long ll;

void solve() {
  int N;
  cin >> N;
  int A[N];
  for (int &i : A)
    cin >> i;

  ll ans = 0;
  const int mod = 1e9 + 7;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += (1ll * A[i] * A[j]) % mod;
      if (ans >= mod)
        ans -= mod;
    }
  }

  cout << ans;
}

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

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  // cout << nl;
  // }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define nl '\n'

using namespace std;

typedef long long ll;

void solve() {
  int N;
  cin >> N;
  int A[N];
  for (int &i : A)
    cin >> i;

  ll ans = 0;
  const int mod = 1e9 + 7;
  int suff[N] = {0};
  suff[N - 1] = A[N - 1];
  for (int i = N - 2; i >= 0; i--)
    suff[i] = (A[i] + suff[i + 1]) % mod;
  for (int i = 0; i < N - 1; i++)
    ans = (ans + (1ll * A[i] * suff[i + 1]) % mod) % mod;

  cout << ans;
}

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

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  // cout << nl;
  // }

  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>

using namespace std;
const int mod = 1e9 + 7;
const long long INF = 1e18;
#define PI 3.141592653589793
#define REP(i, n) for (long long i = 0; i < n; i++)
int main() {
  long long N;
  long long A[100000];
  long long sum = 0;
  long long ans = 0;
  cin >> N;
  REP(i, N) {
    cin >> A[i];
    sum += A[i];
  }
  // cout<<ans<<endl;
  for (int i = 0; i < N; i++) {
    sum -= A[i];
    ans = (A[i] * (sum % mod) + ans) % mod;
    // cout<<ans<<endl;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <string>

using namespace std;
const int mod = 1e9 + 7;
const long long INF = 1e18;
#define PI 3.141592653589793
#define REP(i, n) for (long long i = 0; i < n; i++)
int main() {
  long long N;
  int A[300000];
  long long sum = 0;
  long long ans = 0;
  cin >> N;
  REP(i, N) {
    cin >> A[i];
    sum += A[i];
  }
  // cout<<ans<<endl;
  for (int i = 0; i < N; i++) {
    sum -= A[i];
    ans = (A[i] * (sum % mod) + ans) % mod;
    // cout<<ans<<endl;
  }
  cout << ans;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < ll(b); i++)
#define repr(i, a, b) for (ll i = a; i >= ll(b); i--)
#define endl "\n"
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
#define INF 1e9
#define DEBUG(x) cout << "debug: " << x << endl

const int MOD = 1000000007;

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

  ll ans = 0;
  rep(i, 0, n) {
    ll t = b[n] - b[i + 1];
    t %= MOD;
    ans += a[i] * t;
    ans %= MOD;
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < ll(b); i++)
#define repr(i, a, b) for (ll i = a; i >= ll(b); i--)
#define endl "\n"
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
#define INF 1e9
#define DEBUG(x) cout << "debug: " << x << endl

const int MOD = 1000000007;

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

  ll ans = 0;
  rep(i, 0, n) {
    ll t = b[n] - b[i + 1];
    t %= MOD;
    ans += a[i] * t;
    ans %= MOD;
  }

  cout << ans << endl;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define int long long
#define N 100010
#define M 1010

using namespace std;
const int mod = 1e9 + 7;
int sum[N], a[N], n;

int read() {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    s = s * 10 + (ch ^ 48), ch = getchar();
  return f ? -s : s;
}

signed main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read(), sum[i] = (sum[i - 1] + a[i]) % mod;
  int ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    int xx = (sum[n] - sum[i] + mod) % mod;
    ans = (ans + (a[i] * xx) % mod) % mod;
  }
  cout << ans << "\n";
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define int long long
#define N 200010
#define M 1010

using namespace std;
const int mod = 1e9 + 7;
int sum[N], a[N], n;

int read() {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    s = s * 10 + (ch ^ 48), ch = getchar();
  return f ? -s : s;
}

signed main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read(), sum[i] = (sum[i - 1] + a[i]) % mod;
  int ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    int xx = (sum[n] - sum[i] + mod) % mod;
    ans = (ans + (a[i] * xx) % mod) % mod;
  }
  cout << ans << "\n";
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int a[maxn];
int sum[maxn];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sum[n + 1] = 0;
  for (int i = n; i >= 2; i--) {
    sum[i] = (sum[i + 1] + a[i]) % mod;
  }
  int ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    // cout<<sum[i+1]<<endl;
    ans = (ans + a[i] * sum[i + 1]) % mod;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
int a[maxn];
int sum[maxn];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sum[n + 1] = 0;
  for (int i = n; i >= 2; i--) {
    sum[i] = (sum[i + 1] + a[i]) % mod;
  }
  int ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    // cout<<sum[i+1]<<endl;
    ans = (ans + a[i] * sum[i + 1]) % mod;
  }
  cout << ans << endl;

  return 0;
}
Problem p02572 (Python)

Buggy submission (status: Time Limit Exceeded):
# -*- coding: utf-8 -*-

n = int(input())
nums = list(map(int, input().split()))
x = 10**9 + 7

ans = 0

for i in range(1, n):
    ans += (sum(nums[:i]) % x) * nums[i]
    ans = ans % x

print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

n = int(input())
nums = list(map(int, input().split()))
x = 10**9 + 7

ans = 0

sums = [0] * (n - 1)

for j in range(n - 1):
    if j == 0:
        sums[0] = nums[0] % x
    else:
        sums[j] = (sums[j - 1] + nums[j]) % x

for i in range(1, len(nums)):
    ans += sums[i - 1] * nums[i]
    ans = ans % x

print(ans)
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ull = unsigned long long int;
using ll = long long int;
constexpr ull mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ull> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  ull sum = 0;
  for (int i = 0; i < n - 1; i++) {
    ull t = 0;
    for (int j = i + 1; j < n; j++) {
      t += A[j];
    }
    t = t % mod;
    sum += A[i] * t;
    sum = sum % mod;
  }

  cout << sum << endl;

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ull = unsigned long long int;
using ll = long long int;
constexpr ull mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ull> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

  ull sum = 0;
  ull state = 0;
  for (int i = n - 2; i >= 0; i--) {
    state += A[i + 1];
    state = state % mod;
    sum += state * A[i];
    sum = sum % mod;
  }

  cout << sum << endl;

  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define vite                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define tst                                                                    \
  lli T;                                                                       \
  cin >> T;                                                                    \
  while (T--)
#define mod 1000000007
#define MAX 1e9
#define MIN -1e9
#define lli long long int
#define vt vector
#define pb push_back
int main() {
  vite;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  lli n;
  cin >> n;
  lli a[n];
  for (lli i = 0; i < n; i++)
    cin >> a[i];
  lli sum = 0;
  for (lli i = 0; i < n; i++)
    for (lli j = i + 1; j < n; j++)
      sum = (sum + (a[i] * a[j]) % mod) % mod;

  cout << sum;
  cout << endl;
  // cout<< fixed << setprecision(6) << 1.0 * clock() / CLOCKS_PER_SEC;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define vite                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define tst                                                                    \
  lli T;                                                                       \
  cin >> T;                                                                    \
  while (T--)
#define mod 1000000007
#define MAX 1e9
#define MIN -1e9
#define lli long long int
#define vt vector
#define pb push_back
int main() {
  vite;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  lli n;
  cin >> n;
  lli a[n];
  for (lli i = 0; i < n; i++)
    cin >> a[i];
  lli sum = 0, p[n + 1] = {0};
  for (lli i = n - 1; i >= 0; i--)
    p[i] = (p[i + 1] + a[i]) % mod;
  for (lli i = 0; i < n; i++) {
    sum += a[i] * p[i + 1];
    sum %= mod;
  }
  cout << sum;
  cout << endl;
  // cout<< fixed << setprecision(6) << 1.0 * clock() / CLOCKS_PER_SEC;
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a[100005];

// ------------------------------------
// 1 is true
// 0 is false
// ------------------------------------

ll binexp(ll x, ll n, ll m) {
  x %= m;
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    n >>= 1;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  const ll k = 1000000007;
  ll sumai = 0, sumaisq = 0, minvtwo = binexp(2, (k - 2), k);
  for (int i = 0; i < n; i++) {
    sumai += a[i] % k;
    sumaisq += ((a[i] % k) * (a[i] % k)) % k;
    sumai = sumai % k;
    sumaisq = sumaisq % k;
  }

  cout << (minvtwo * ((sumai * sumai - sumaisq) % k)) % k << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a[200005];

// ------------------------------------
// 1 is true
// 0 is false
// ------------------------------------

ll binexp(ll x, ll n, ll m) {
  x %= m;
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    n >>= 1;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  const ll k = 1000000007;
  ll sumai = 0, sumaisq = 0, minvtwo = binexp(2, (k - 2), k);
  for (int i = 0; i < n; i++) {
    sumai += a[i] % k;
    sumaisq += ((a[i] % k) * (a[i] % k)) % k;
    sumai = sumai % k;
    sumaisq = sumaisq % k;
  }

  cout << (minvtwo * ((sumai * sumai - sumaisq) % k)) % k << endl;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "cstring"
#include "iostream"
#include "queue"
#include "stack"
using namespace std;

#define read(x) scanf("%d", &x)
#define readl(x) scanf("%lld", &x)
#define ll long long
#define ull unsigned long long
#define MOD 1000000007

int n;
ll a[100005];
ll sum[100005];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = n; i >= 1; i--) {
    sum[i] = (sum[i + 1] + a[i]) % MOD;
  }
  for (int i = 1; i < n; i++) {
    sum[0] = (sum[0] + a[i] * sum[i + 1]) % MOD;
  }
  printf("%lld\n", sum[0] % MOD);
  return 0;
}

Fixed submission:
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "cstring"
#include "iostream"
#include "queue"
#include "stack"
using namespace std;

#define read(x) scanf("%d", &x)
#define readl(x) scanf("%lld", &x)
#define ll long long
#define ull unsigned long long
#define MOD 1000000007

int n;
ll a[200005];
ll sum[200005];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = n; i >= 1; i--) {
    sum[i] = (sum[i + 1] + a[i]) % MOD;
  }
  for (int i = 1; i < n; i++) {
    sum[0] = (sum[0] + a[i] * sum[i + 1]) % MOD;
  }
  printf("%lld\n", sum[0] % MOD);
  return 0;
}
Problem p02572 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define P 1000000007

int main() {
  // input
  int64_t N;
  cin >> N;
  vector<int64_t> A(N);
  rep(i, N) { cin >> A.at(i); }
  // calc
  int64_t ans = 0;
  rep(i, N - 1) {
    for (int64_t j = i + 1; j < N; j++) {
      ans += A.at(i) * A.at(j);
      ans %= P;
    }
  }
  // output
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define P 1000000007

int main() {
  // input
  int64_t N;
  cin >> N;
  vector<int64_t> A(N);
  rep(i, N) { cin >> A.at(i); }
  // calc
  int64_t ans = 0;
  int64_t S = 0;
  for (int i = N - 2; i >= 0; i--) {
    S += A.at(i + 1);
    S %= P;
    ans += A.at(i) * S;
    ans %= P;
  }
  // output
  cout << ans << endl;
}
Problem p02572 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <string.h>
#define mod 1000000007
using namespace std;

long long a[100100];
long long h, n;

int main() {
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
    h += a[i];
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    h -= a[i];
    ans = (ans + ((h) % mod * a[i]) % mod) % mod;
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <string.h>
#define mod 1000000007
using namespace std;

long long a[10010000];
long long h, n;

int main() {
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
    h += a[i];
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    h -= a[i];
    ans = (ans + ((h) % mod * a[i]) % mod) % mod;
  }
  printf("%lld\n", ans);
}