Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #define _CRT_SECURE_NO_WARNINGS
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <string>
- #include <map>
- #include <set>
- #include <algorithm>
- #include <vector>
- #include <stdio.h>
- #include <cmath>
- #include <math.h>
- #include <queue>
- #include <stack>
- #include <climits>
- #include <deque>
- #include <ctime>
- #include <iomanip>
- #include <bitset>
- #include <unordered_map>
- #include <unordered_set>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef unsigned int ui;
- #define mh() make_heap()
- #define poph() pop_heap()
- #define pushh() push_heap()
- #define sor(n) n.begin(), n.end()
- #define rsor(n) n.rbegin(), n.rend()
- #define mp make_pair
- #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
- #define p(T) pair<T,T>
- #define toch(x) cout.precision(x), cout.setf(ios::fixed)
- #define znac(l) abs(l)/l
- #define IOS ios::sync_with_stdio(false)
- #define IOSB cin.tie(0), cout.tie(0);
- const ll ok = ll(1e9 + 7);
- int main()
- {
- IOSB;
- IOS;
- #ifdef TheCompiler
- files;
- #endif
- ll n;
- map<string, ll>prod;
- cin >> n;
- for (int i = 1; i <= n; i++)
- {
- ll a;
- string s;
- cin >> a;
- cin >> s >> s;
- prod[s] = a;
- }
- cin >> n;
- queue<pair<ll,string>>q;
- for (int i = 1; i <= n; i++)
- {
- ll a;
- string s;
- cin >> a;
- cin >> s >> s;
- q.push({ a,s });
- }
- q.push({ -1,"" });
- deque<pair<ll,string>>now;
- now.push_front(q.front());
- q.pop();
- ll time = 0;
- while (!now.empty())
- {
- if (now.front().first == -1)
- {
- break;
- }
- time++;
- if (prod.find(now.front().second) != prod.end() && prod[now.front().second] != 0)
- {
- if (prod[now.front().second] >= now.front().first)
- {
- prod[now.front().second] -= now.front().first;
- now.pop_front();
- }
- else
- {
- if (now.size() == 1)
- {
- pair<ll, string>nw = now.front();
- now.pop_front();
- nw.first = prod[nw.second];
- now.push_front(nw);
- now.push_front(q.front());
- q.pop();
- }
- else
- {
- pair<ll, string>nw = now.front();
- now.pop_front();
- nw.first = prod[nw.second];
- now.push_back(nw);
- }
- }
- }
- else
- now.pop_front();
- if (now.empty())
- {
- now.push_front(q.front());
- q.pop();
- }
- }
- cout << time;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement