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 MOD 1000000007
#define FASTIO                                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)

ll arr[200001];
ll sum[200001];

int main() {
  FASTIO;
  int N;
  ll ans = 0;
  cin >> N;
  FOR(i, 1, N) { cin >> arr[i]; }
  sum[N] = arr[N];
  ROF(i, N - 1, 1) { sum[i] = (sum[i + 1] + arr[i]) % MOD; }
  FOR(i, 1, N - 1) { ans = (ans + arr[i] * sum[i + 1]) % MOD; }
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long int ll;
#define MOD 1000000007
#define FASTIO                                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)

ll arr[200001];

int main() {
  FASTIO;
  int N;
  ll sum = 0;
  cin >> N;
  FOR(i, 1, N)
  cin >> arr[i];
  FOR(i, 1, N) {
    FOR(j, i + 1, N) { sum = (sum + arr[i] * arr[j]) % MOD; }
  }
  cout << sum;

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

#define sz(x) (int)(x.size())

typedef long long ll;
typedef tuple<int, int, int> tri;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>,
                         __gnu_pbds::rb_tree_tag,
                         __gnu_pbds::tree_order_statistics_node_update>
    ordered_set;

const ll MODULO = 1000000007;

void ftime() {
#ifndef ONLINE_JUDGE
  cout << "\\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\\n";
#endif
}

static int speed_increase = []() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  return 0;
}();

vll arr(2e5 + 1);
vll pre(2e5 + 1);
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i];
  for (int i = 1; i <= n; ++i) {
    pre[i] = (pre[i - 1] + arr[i]) % MODULO;
  }
  ll sum = 0;
  for (int i = n; i > 1; --i) {
    sum = (pre[i - 1] * arr[i] + sum) % MODULO;
  }
  cout << sum << "\\n";
}

int main() {
  solve();
  // ftime();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

#define sz(x) (int)(x.size())

typedef long long ll;
typedef tuple<int, int, int> tri;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>,
                         __gnu_pbds::rb_tree_tag,
                         __gnu_pbds::tree_order_statistics_node_update>
    ordered_set;

const ll MODULO = 1000000007;

void ftime() {
#ifndef ONLINE_JUDGE
  cout << "\\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\\n";
#endif
}

static int speed_increase = []() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  return 0;
}();

vll arr(2e5 + 1);
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  ll sum = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      sum += (arr[i] * arr[j]) % MODULO;
      sum = (sum) % MODULO;
    }
  }
  cout << sum << "\\n";
}

int main() {
  solve();
  // ftime();
  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 RREP(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  ll ans = 0;
  ll x = 0;
  REP(i, n) {
    ans = (ans + a[i] * x) % MOD;
    x = (x + a[i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) { cin >> a[i]; }
  ll ans = 0;
  ll t = 1e9 + 7;
  REP(i, n) {
    FOR(j, i + 1, n) { ans = (ans + a[i] * a[j]) % t; }
  }
  cout << ans;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^{-1} mod を計算する
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

int main() {
  int64_t x = 1000000007;
  int N;
  cin >> N;
  vector<int64_t> A(N);

  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int64_t S;
  /*
    for (int i = 0; i < N-1; i++){
      for (int j = i+1; j < N; j++){
        S += (A[i] * A[j])%x;
      }
    }
  */

  int64_t Sum = 0;
  int64_t SquareSum = 0;
  for (int i = 0; i < N; i++) {
    Sum += A[i];
    Sum = Sum % x;
    SquareSum += (A[i] * A[i]);
    SquareSum = SquareSum % x;
  }

  S = ((Sum * Sum) - SquareSum) % x;
  // cout << S << endl;
  if (S < 0) {
    S += x;
  }
  // cout << modinv(2,x) << endl;
  S = S * modinv(2, x);

  int64_t ans = S % x;

  cout << ans;
}


Error: TLE

Fixed submission:

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

int main() {
  int64_t x = 1000000000 + 7;
  int N;
  cin >> N;
  vector<int64_t> A(N);

  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int64_t S;

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

  int64_t ans = S % x;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define mk make_pair
#define pi pair<int, int>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = a; i >= (b); --i)
using namespace std;
int dt[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {0, 0}};
// typedef pair<int, int> P;
// priority_queue<int, vector<int>, greater<int> > q;

const int N = 2e5 + 5;
ll a[N];
ll suf[N];

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define mk make_pair
#define pi pair<int, int>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = a; i >= (b); --i)
using namespace std;
int dt[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {0, 0}};
// typedef pair<int, int> P;
// priority_queue<int, vector<int>, greater<int> > q;

const int N = 2e5 + 5;

int n;
ll a[N];

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

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

int main() {
  ll con = 1000000007;
  int n;
  cin >> n;
  vector<ll> a(n);
  ll cnt = 0;
  ll ans = 0;
  rep(i, n) {
    cin >> a[i];
    cnt += a[i];
  }

  rep(i, n - 1) {
    cnt -= a[i];
    ans += (cnt % con) * a[i];
    ans %= con;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

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

int main() {
  ll con = 1e9 + 7;

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

  rep(i, n - 1) {
    for (int j = i + 1; j < n; j++) {
      ans += a[i] * a[j];
      ans %= con;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
#define pb emplace_back
#define LOCAL

using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
typedef long long ll;
typedef pair<int, int> Pii;
const ll mod = 1e9 + 7;

template <typename T> inline void read(T &t) {
  char c = getchar();
  t = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c))
    t = t * 10 + c - 48, c = getchar();
  t = f * t;
}

template <typename T, typename... Args> inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}

ll a[maxn], sum[maxn];

int main() {
  int n;
  read(n);
  for (int i = 1; i <= n; i++) {
    read(a[i]);
  }
  for (int i = n; i >= 1; i--) {
    sum[i] = (a[i] + sum[i + 1]) % mod;
  }
  ll ans = 0;
  for (int i = n - 1; i >= 1; i--) {
    ans = (ans + sum[i + 1] * a[i] % mod) % mod;
  }
  printf("%lld\\n", ans % mod);
  return 0;
}

Error: 0

Fixed submission:

// #include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#define pb emplace_back
#define LOCAL

using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
typedef long long ll;
typedef pair<int, int> Pii;
const ll mod = 1e9 + 7;

template <typename T> inline void read(T &t) {
  char c = getchar();
  t = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c))
    t = t * 10 + c - 48, c = getchar();
  t = f * t;
}

template <typename T, typename... Args> inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}

ll a[maxn];

int main() {
  int n;
  read(n);
  for (int i = 1; i <= n; i++) {
    read(a[i]);
  }
  for (int i = n - 1; i >= 1; i--) {
    a[i] = (a[i] + a[i + 1]) % mod;
  }
  ll ans = 0;
  for (int i = n - 1; i >= 1; i--) {
    ll tmp = (a[i] - a[i + 1]) % mod;
    ans = (ans + ((tmp) % mod * a[i + 1] % mod) % mod) % mod;
  }
  printf("%lld\\n", ans % mod);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long int
#define endl '\\n'
#define inf 1e18
#define mod 1000000007

void testCase() {
  // ll t; cin>>t; while(t--)
  {
    ll n;
    cin >> n;
    ll ar[n + 5];
    for (int i = 1; i <= n; i++)
      cin >> ar[i];
    ll sum = ar[n];
    ll ans = 0;
    for (int i = n - 1; i >= 1; i--) {
      ans = (ans + (ar[i] * sum) % mod) % mod;
      sum = (sum + ar[i]) % mod;
    }
    cout << ans;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  testCase();
  return 0;
}

Error: WA

Fixed submission:

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

#define ll unsigned long long int
#define endl '\\n'
#define inf 1e18
#define mod 1000000007

void testCase() {
  // ll t; cin>>t; while(t--)
  {
    ll n;
    cin >> n;
    ll ar[n];
    for (int i = 1; i <= n; i++)
      cin >> ar[i];
    ll sum = ar[n];
    ll ans = 0;
    for (int i = n - 1; i >= 1; i--) {
      ans = (ans + (ar[i] * sum) % mod) % mod;
      sum = (sum + ar[i]) % mod;
    }
    cout << ans;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  testCase();
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

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


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
using ll = long long;
using ull = unsigned long long;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  ll mod = 1000000007;
  vector<ll> a(n), suf(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  suf[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    suf[i] = suf[i + 1] + a[i];
    suf[i] %= mod;
  }
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    ans += a[i - 1] * suf[i];
    ans %= mod;
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
using ll = long long;
using ull = unsigned long long;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int mod = 1000000007;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll s = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      s += a[i] * a[j];
      s %= mod;
    }
  cout << s;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define l(i, a, b) for (auto i = a; i < b; i++)
#define lr(i, a, b) for (auto i = a; i >= b; i--)
#define ll long long
#define lli long long int
#define ld long double
#define vec vector<int>
#define mii map<ll, ll>
#define vl vector<long long>
#define se set<long long int>
#define pi pair<long long, long long>
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define all(v) v.begin(), v.end()
#define in(x) cin >> x
#define o(x) cout << x
#define SYNC                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define PI 3.14159265359
using namespace std;

ll modI(ll a, ll m) {
  ll m0 = m, y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m;
    a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}

// const string k = "enterstringtobechecked";

int main() {
  SYNC
      // clock_t tStart = clock();
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      ll n;
  in(n);
  vl v(n);
  l(i, 0, n) { in(v[i]); }
  ll ans = 0, sum = 0;
  ll MOD = 1e9 + 7;
  l(i, 0, n) {
    ans += (ll)(v[i] * sum);
    ans %= MOD;
    sum += v[i];
    sum %= MOD;
  }
  o(ans) << endl;

  // printf("Time taken: %.2fs\\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define l(i, a, b) for (auto i = a; i < b; i++)
#define lr(i, a, b) for (auto i = a; i >= b; i--)
#define ll long long
#define lli long long int
#define ld long double
#define vec vector<int>
#define mii map<ll, ll>
#define vl vector<long long>
#define se set<long long int>
#define pi pair<long long, long long>
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define all(v) v.begin(), v.end()
#define in(x) cin >> x
#define o(x) cout << x
#define SYNC                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define PI 3.14159265359
using namespace std;

ll max_rec(vl v, ll i, ll n) {
  if (i >= n)
    return 0;
  else
    return max(max_rec(v, i + 2, n) + v[i], max_rec(v, i + 1, n));
}

int main() {
  SYNC
      // clock_t tStart = clock();
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      ll n;
  in(n);
  vl v(n);
  ll MOD = 1e9 + 7;
  l(i, 0, n) { in(v[i]); }
  ll ans = 0;
  l(i, 0, n) {
    for (ll j = i + 1; j < n; j++) {
      ans += (v[i] * v[j]) % MOD;
      ans = ans % MOD;
    }
  }
  o(ans) << endl;

  // printf("Time taken: %.2fs\\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
const ll mods = 1e9 + 7;

int main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<ll> s(n + 1, 0);
  rep(i, n) cin >> a[i];
  rep(i, n) s[i + 1] = s[i] + a[i];
  ll ans = 0;
  rep(i, n) {
    ll num = (s[n] - s[i + 1]) % mods;
    ans += a[i] * num;
    ans %= mods;
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
const ll mods = 1e9 + 7;

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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

long long MOD = 1e9 + 7;

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

  long long s = 0;
  for (int i = 0; i < n; i++) {
    sum -= a[i];
    s += (a[i] * (sum % MOD)) % MOD;
  }

  cout << s % MOD << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

long long MOD = 1e9 + 7;

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

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

  cout << s % MOD << '\\n';

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

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

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

Error: TLE

Fixed submission:

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

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

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

  vector<ll> b(n + 1, 0);
  rep(i, n) b[i + 1] = b[i] + v[i];

  ll sum = 0, ans = 0;
  rep(i, n) {
    sum = (b[n] - b[i + 1]) % m;
    ans += v[i] * sum;
    ans %= m;
  }
  cout << ans % m << endl;
}

Error: TLE

Fixed submission:

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

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