/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Egor Kulikov */ #include #include #ifndef JHELPER_EXAMPLE_PROJECT_INPUT_H #define JHELPER_EXAMPLE_PROJECT_INPUT_H // // Created by egor on 30.10.2019. // #ifndef JHELPER_EXAMPLE_PROJECT_GENERAL_H #define JHELPER_EXAMPLE_PROJECT_GENERAL_H #include using namespace std; #define all(v) (v).begin(), (v).end() typedef long long ll; typedef vector vi; typedef pair pii; const int MAX_INT = 2147483647; const double PI = atan(1) * 4; const int DX_KNIGHT[] = {2, 1, -1, -2, -2, -1, 1, 2}; const int DY_KNIGHT[] = {1, 2, 2, 1, -1, -2, -2, -1}; const int DX4[] = {1, 0, -1, 0}; const int DY4[] = {0, 1, 0, -1}; bool isValidCell(int x, int y, int rows, int cols) { return x >= 0 && y >= 0 && x < rows && y < cols; } void decreaseByOne() { } template void decreaseByOne(vector& arr, Vargs&...arrs) { for (int& i : arr) { i--; } decreaseByOne(arrs...); } template T minim(T& was, T cand) { return was = min(was, cand); } template T maxim(T& was, T cand) { return was = max(was, cand); } inline int bitCount(int number) { return __builtin_popcount(number); } inline int bitCount(ll number) { return __builtin_popcount(number); } #endif //JHELPER_EXAMPLE_PROJECT_GENERAL_H class Input { public: enum ErrorType { OK, END_OF_FILE, UNEXPECTED_SYMBOL, INVALID_CALL }; private: istream& in; bool exhausted = false; ErrorType error = OK; int get(); template T readInteger(); int skipWhitespace(); public: Input(istream& in); int readInt(); ll readLong(); string readString(); vector readIntArray(int size); template T readType(); template pair readType(); template vector readArray(int size); template tuple, vector > readArrays(int size); template tuple, vector, vector > readArrays(int size); template tuple, vector, vector, vector > readArrays(int size); template tuple, vector, vector, vector, vector > readArrays(int size); template vector > readTable(int rows, int cols); string readLine(); }; inline bool isWhitespace(int c) { return isspace(c) || c == EOF; } int Input::skipWhitespace() { int c; do { c = get(); if (exhausted) { error = END_OF_FILE; return c; } } while (isWhitespace(c)); return c; } Input::Input(std::istream &in) : in(in) { } inline int Input::get() { int result = in.get(); if (result == EOF) { exhausted = true; } return result; } template T Input::readType() { error = INVALID_CALL; return nullptr; } template T Input::readInteger() { error = OK; int c = skipWhitespace(); if (error != OK) { return 0; } int sgn = 1; if (c == '-') { sgn = -1; c = get(); } T res = 0; do { if (!isdigit(c)) { error = UNEXPECTED_SYMBOL; return 0; } res *= 10; res += c - '0'; c = get(); } while (!isWhitespace(c)); return res * sgn; } template<> int Input::readType() { return readInteger(); } template<> ll Input::readType() { return readInteger(); } template<> char Input::readType() { error = OK; int c = skipWhitespace(); if (error != OK) { return 0; } return c; } template<> string Input::readType() { error = OK; int c = skipWhitespace(); if (error != OK) { return ""; } vector res; do { if (error != OK) { return ""; } res.push_back(c); } while (!isWhitespace(c = get())); return string(res.begin(), res.end()); } inline int Input::readInt() { return readType(); } inline ll Input::readLong() { return readType(); } template vector Input::readArray(int size) { vector res; res.reserve(size); for (int i = 0; i < size; i++) { res.push_back(readType()); if (error != OK) { res.clear(); return res; } } return res; } vector Input::readIntArray(int size) { return readArray(size); } template tuple, vector > Input::readArrays(int size) { vector v1; vector v2; v1.reserve(size); v2.reserve(size); for (int i = 0; i < size; ++i) { v1.push_back(readType()); v2.push_back(readType()); } return make_tuple(v1, v2); } string Input::readString() { return readType(); } template vector> Input::readTable(int rows, int cols) { vector > result; result.reserve(rows); for (int i = 0; i < rows; ++i) { result.push_back(readArray(cols)); } return result; } string Input::readLine() { error = OK; int c = skipWhitespace(); if (error != OK) { return ""; } int length = 0; vector res; do { if (error != OK) { return ""; } res.push_back(c); if (!isWhitespace(c)) { length = res.size(); } c = get(); } while (c != '\n' && c != '\r' && c != EOF); return string(res.begin(), res.begin() + length); } template tuple, vector, vector > Input::readArrays(int size) { vector v1; vector v2; vector v3; v1.reserve(size); v2.reserve(size); v3.reserve(size); for (int i = 0; i < size; ++i) { v1.push_back(readType()); v2.push_back(readType()); v3.push_back(readType()); } return make_tuple(v1, v2, v3); } template tuple, vector, vector, vector > Input::readArrays(int size) { vector v1; vector v2; vector v3; vector v4; v1.reserve(size); v2.reserve(size); v3.reserve(size); v4.reserve(size); for (int i = 0; i < size; ++i) { v1.push_back(readType()); v2.push_back(readType()); v3.push_back(readType()); v4.push_back(readType()); } return make_tuple(v1, v2, v3, v4); } template tuple, vector, vector, vector, vector > Input::readArrays(int size) { vector v1; vector v2; vector v3; vector v4; vector v5; v1.reserve(size); v2.reserve(size); v3.reserve(size); v4.reserve(size); v5.reserve(size); for (int i = 0; i < size; ++i) { v1.push_back(readType()); v2.push_back(readType()); v3.push_back(readType()); v4.push_back(readType()); v5.push_back(readType()); } return make_tuple(v1, v2, v3, v4, v5); } #endif //JHELPER_EXAMPLE_PROJECT_INPUT_H // // Created by egor on 31.10.2019. // #ifndef JHELPER_EXAMPLE_PROJECT_OUTPUT_H #define JHELPER_EXAMPLE_PROJECT_OUTPUT_H class Output { private: ostream& out; template void printSingle(const T& value); template void printSingle(const vector& value); template void printSingle(const pair& value); public: Output(ostream& out); void print(); templatevoid print(const T& first, const Targs... args); templatevoid printLine(const Targs... args); void flush(); }; Output::Output(ostream &out) : out(out) { out << setprecision(12); } void Output::print() { } template void Output::print(const T& first, const Targs... args) { printSingle(first); if (sizeof...(args) != 0) { out << ' '; print(args...); } } template void Output::printSingle(const T& value) { out << value; } template void Output::printLine(const Targs... args) { print(args...); out << '\n'; } void Output::flush() { out.flush(); } template void Output::printSingle(const vector& array) { unsigned int size = array.size(); for (int i = 0; i < size; ++i) { out << array[i]; if (i + 1 != size) { out << ' '; } } } template void Output::printSingle(const pair& value) { out << value.first << ' ' << value.second; } #endif //JHELPER_EXAMPLE_PROJECT_OUTPUT_H // // Created by egor on 31.10.2019. // #ifndef JHELPER_EXAMPLE_PROJECT_ALGO_H #define JHELPER_EXAMPLE_PROJECT_ALGO_H template inline void unique(vector& v) { v.resize(unique(all(v)) - v.begin()); } vi createOrder(int n) { vi order(n); for (int i = 0; i < n; i++) { order[i] = i; } return order; } template inline vector > makeArray(int a, int b) { return vector >(a, vector(b)); } //template //inline vector > > makeArray(int a, int b, int c) { // return vector > >(a, makeArray(b, c)); //} template inline vector > makeArray(int a, int b, T init) { return vector >(a, vector(b, init)); } template inline vector > > makeArray(int a, int b, int c, T init) { return vector > >(a, makeArray(b, c, init)); } template inline void addAll(vector& v, const vector& toAdd) { v.insert(v.end(), toAdd.begin(), toAdd.end()); } #endif //JHELPER_EXAMPLE_PROJECT_ALGO_H class AntScheduling { public: void solve(istream& inp, ostream& outp) { Input in(inp); Output out(outp); int n = in.readInt(); vector > left; vector > right; for (int i = 0; i < n; ++i) { char direction = in.readType(); int a = in.readInt(); int t = in.readInt(); if (direction == 'L') { left.emplace_back(a, t); } else { right.emplace_back(a, t); } } sort(all(left)); sort(all(right)); auto ansLeft = makeArray(left.size() + 1, right.size() + 1, 1000000000); auto ansRight = makeArray(left.size() + 1, right.size() + 1, 1000000000); ansLeft[0][0] = ansRight[0][0] = 0; for (int i = 0; i <= left.size(); i++) { for (int j = 0; j <= right.size(); j++) { int start = ansRight[i][j] - 10; int end = start; for (int l = i; l < left.size(); l++) { start = max(start + 10, left[l].first); end = max(end + 10, start + left[l].second); minim(ansLeft[l + 1][j], end); } start = ansLeft[i][j] - 10; end = start; for (int l = j; l < right.size(); l++) { start = max(start + 10, right[l].first); end = max(end + 10, start + right[l].second); minim(ansRight[i][l + 1], end); } } } out.printLine(min(ansLeft[left.size()][right.size()], ansRight[left.size()][right.size()])); } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); AntScheduling solver; std::istream& in(std::cin); std::ostream& out(std::cout); int n; in >> n; for(int i = 0; i < n; ++i) { solver.solve(in, out); } return 0; }