Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- void __print(int x) {cerr << x;}
- void __print(long x) {cerr << x;}
- void __print(long long x) {cerr << x;}
- void __print(unsigned x) {cerr << x;}
- void __print(unsigned long x) {cerr << x;}
- void __print(unsigned long long x) {cerr << x;}
- void __print(float x) {cerr << x;}
- void __print(double x) {cerr << x;}
- void __print(long double x) {cerr << x;}
- void __print(char x) {cerr << '\'' << x << '\'';}
- void __print(const char *x) {cerr << '\"' << x << '\"';}
- void __print(const string &x) {cerr << '\"' << x << '\"';}
- void __print(bool x) {cerr << (x ? "true" : "false");}
- template<typename T, typename V>
- void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
- template<typename T>
- void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
- void _print() {cerr << "]\n";}
- template <typename T, typename... V>
- void print(T t, V... v) {_print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
- #ifndef ONLINE_JUDGE
- #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
- #else
- #define debug(x...)
- #endif
- #define len(v) (int)(v).size()
- #define all(v) (v).begin(),(v).end()
- #define forn(i, n) for (int i=0;i<n;++i)
- using val = int64_t;
- using ii = pair<val,int>;
- const char nl = '\n';
- const int mod = 1000000007;
- int main() {
- cin.tie(nullptr)->sync_with_stdio(false);
- int n;
- cin >> n;
- deque<val> a, b;
- forn (i, n) {
- val x;
- int dir;
- cin >> x >> dir;
- if (dir) a.push_back(x);
- else b.push_back(x);
- }
- val tempo = 0;
- while (!(a.empty() && b.empty())) {
- if (a.empty()) {
- if (tempo < b.front()) {
- tempo = b.front();
- }
- val q = b.front();
- b.pop_front();
- while (!b.empty() && q < tempo) {
- q = b.front();
- b.pop_front();
- }
- if (q < tempo) {
- tempo += 10;
- } else {
- if (!b.empty()) {
- tempo += b.back() - q + 10;
- } else {
- tempo += 10;
- }
- }
- break;
- } else if (b.empty()) {
- if (tempo < a.front()) {
- tempo = a.front();
- }
- val q = a.front();
- a.pop_front();
- while (!a.empty() && q < tempo) {
- q = a.front();
- a.pop_front();
- }
- if (q < tempo) {
- tempo += 10;
- } else {
- if (!a.empty()) {
- tempo += a.back() - q + 10;
- } else {
- tempo += 10;
- }
- }
- break;
- } else {
- if (a.front() < b.front()) {
- val q = a.front();
- a.pop_front();
- val diff = 0;
- if (tempo < q) {
- tempo = q;
- }
- while (!a.empty() && a.front() - q <= 10) {
- diff += max(a.front() - max(tempo, q), (val)0);
- q = a.front();
- a.pop_front();
- }
- tempo += 10 + diff;
- } else {
- val q = b.front();
- b.pop_front();
- val diff = 0;
- if (tempo < q) {
- tempo = q;
- }
- while (!b.empty() && b.front() - q <= 10) {
- diff += max(b.front() - max(tempo, q), (val)0);
- q = b.front();
- b.pop_front();
- }
- tempo += 10 + diff;
- }
- }
- }
- cout << tempo << nl;
- return 0;
- }
Add Comment
Please, Sign In to add comment