Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <stack>
- #include <queue>
- using namespace std;
- int main() {
- string line;
- getline(cin, line);
- istringstream ss(line);
- stack<int> numbers;
- int number;
- while (ss >> number) {
- numbers.push(number);
- }
- queue<string> q;
- while (true) {
- getline(cin, line);
- istringstream ss(line);
- string s;
- while (ss >> s) {
- q.push(s);
- }
- string command = q.front();
- q.pop();
- if (command == "end") {
- break;
- }
- else if (command == "add") {
- while (!q.empty()) {
- numbers.push(stoi(q.front()));
- q.pop();
- }
- }
- else {
- int num = stoi(q.front());
- q.pop();
- if (numbers.size() > num) {
- while (!numbers.empty() && num-- != 0) {
- numbers.pop();
- }
- }
- }
- }
- int sum = 0;
- while (!numbers.empty()) {
- sum += numbers.top();
- numbers.pop();
- }
- cout << sum << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement