#include #include #include #include #include #include #include "../../debugging.h" using namespace std; // ----- FILE IO ----- #define STDIN(filename) freopen(filename, "r", stdin) #define STDOUT(filename) freopen(filename, "w", stdout) #define NL '\n' // ----- FOR LOOPS ----- #define F(i,L,R) for (int i = L; i < R; i++) #define FE(i,L,R) for (int i = L; i <= R; i++) #define FD(i,L,R) for (int i = L; i > R; i--) #define FDE(i,L,R) for (int i = L; i >= R; i--) // ----- WEZ ----- #define takeI(n) int (n); cin >> n #define takeII(n,m) int (n),(m); cin >> n >> m #define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k) #define whileZ takeI(T); while(T--) // // ----- MAX MIN ----- #define REMAX(a,b) (a)=max((a),(b)) #define REMIN(a,b) (a)=min((a),(b)) // ----- DEBUG ----- #define DBG(vari) cerr<<#vari<<" = "<<(vari)<, int> seen; int x = 0, y = 0, t = 0; seen.insert({{x, y}, t}); int maxRes = INT_MAX; whileZ { char dir; int steps; cin >> dir >> steps; switch (dir) { case 'N': F(i, 0, steps) { t++; y++; if (seen.count({x,y})) maxRes = min(maxRes, t - seen[{x,y}]); seen.insert({{x, y}, t}); } case 'S': F(i, 0, steps) { t++; y--; if (seen.count({x,y})) maxRes = min(maxRes, t - seen[{x,y}]); seen.insert({{x, y}, t}); } case 'E': F(i, 0, steps) { t++; x++; if (seen.count({x,y})) maxRes = min(maxRes, t - seen[{x,y}]); seen.insert({{x, y}, t}); } case 'W': F(i, 0, steps) { t++; x--; if (seen.count({x,y})) maxRes = min(maxRes, t - seen[{x,y}]); seen.insert({{x, y}, t}); } } } cerr << seen ; cout << maxRes << NL; return 0; }