I was solving this problem from the last 2 hour almost ->
1334C - Circle of MonstersI made quite submissions but I was getting TLE for my code.
Then I looked for some red coders solution and the thing is the logic and implementation was same, but I still submit the red coder's code and I'm surprised that IT STILL GOT TLE WHY ?
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
void solve() {
ll n; cin >> n;
vector<ll> a(n), b(n);
for (ll i = 0; i < n; i++)
cin >> a[i] >> b[i];
vector<ll> cost(n, 0);
for (ll i = 0; i < n; i++)
cost[i] = max(0LL, a[i] - b[(i + n - 1) % n]);
ll sum = accumulate(cost.begin(), cost.end(), 0LL);
ll ans = (ll)1e18;
for (ll i = 0; i < n; i++) {
ans = min(ans, sum - cost[i] + a[i]);
}
cout << ans << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll tc;
cin >> tc;
while (tc--)
solve();
return 0;
}
I don't know what's the problem in this very simple and clean code and still I'm facing this issue.
Can anyone please consider this problem and tell me, why it is giving me TLE ?
Use fast IO
Yeah It works. But How ? What's the purpose of this, although the code was in perfect O(N).
The maximum amount of input for this problem is around 1e6, you need fastIO for data like this. cin/cout are much slower than printf that they may cause TLE unless you add ios::sync_with_stdio(0)
by the way you should never use endl, it's even slower than cin/cout because some buffer stuff, use '\n' instead.
"never" is a strong word. endl could be used in interactives.