#include #include #include #include using namespace std; class Directory { public: Directory *parent = nullptr; unordered_map dirs; long direct_size = 0; void mkdir(string name) { dirs[name] = new Directory(); dirs[name]->parent = this; } void mkfile(string name, long size) { (void) name; direct_size += size; } Directory* cd(string path) { return parent; } }; template long getSizes(Directory *dir, F&& fn) { long size = dir->direct_size; for (auto &c : dir->dirs) { size += getSizes(c.second, fn); } fn(size); return size; } int main() { Directory root; Directory *pwd = &root; for (string line; getline(cin, line); ) { if (line.compare(0, 4, "$ cd") == 0) { string path = line.substr(5); if (path == "/") { pwd = &root; } else if (path == "..") { pwd = pwd->parent; } else { pwd = pwd->dirs[path]; } if (pwd == nullptr) throw "bad input"; } if (line[0] != '$') { int pos = line.find(" "); string meta = line.substr(0, pos); string name = line.substr(pos + 1); if (meta == "dir") { pwd->mkdir(name); } else { pwd->mkfile(name, stol(meta)); } } } long total1 = 0; getSizes(&root, [&](long size){ if (size <= 100000) total1 += size; }); cout << total1 << endl; }