#include "bits/stdc++.h" #include using namespace std; using namespace atcoder; using ll = long long; using vl = vector; using pl = pair; using PQ = priority_queue; using PQR = priority_queue, greater<>>; struct edge {long long to;long long cost;edge(long long t,long long c):to(t),cost(c){}}; using Graph = vector>; using WGraph = vector>; constexpr int dx[8]{1,0,-1,0,1,1,-1,-1}; constexpr int dy[8]{0,1,0,-1,1,-1,1,-1}; #define rep(i, n) for(long long (i) = 0; (i) < (n); (i)++) #define all(v) (v).begin(), (v).end() #define pb emplace_back #define V vector #define uniquify(vec) sort(all(vec)); (vec).erase(std::unique((vec).begin(), (vec).end()), (vec).end()) constexpr long long MOD1 = 1e9 + 7; constexpr long long MOD9 = 998244353; constexpr long long INF = MOD1*MOD1+1; constexpr long long LL_MAX = std::numeric_limits::max(); long long safe_mod(long long n, long long m){if(n >= 0) {return n % m;} else return (n%m + m)%m;} long long div_ceil(long long a, long long b){return (a + b - 1) / b;} long long DIG(long long x, long long d){ll ret=0;while(x){x/=d;ret++;}return ret;} long long pw(long long x,long long n) {ll ret=1;while(n>0){if(n&1){ret=ret*x;}if(n>>=1){x=x*x;}}return ret;} template T SUM(const vector &v){T ret{};return(accumulate(all(v), ret));} template T pwmod(T x, long long n, long long m) {T ret=1;while(n>0){if(n&1)ret=ret*x%m;x=x*x%m;n>>=1;}return ret%m;} template void rsort(vector &v) {sort(all(v), greater());} template bool chmax(T &a,const T& b) {if(a bool chmin(T &a,const T& b) {if(a>b){a=b;return true;}return false;} template void fin(const T &a) {cout << a << '\n';exit(0);} template pair mp(const T &a, const S &b){return make_pair(a, b);} template void unique(vector &a){sort(all(a)); a.erase(std::unique(a.begin(), a.end()), a.end());} template vector compress(vector &a){ int n = a.size(); vector ret(n); vector temp = a; unique(a); for(int i = 0; i < n; i++) ret[i] = lower_bound(a.begin(), a.end(), temp[i]) - a.begin(); return ret; } std::ostream &operator<<(std::ostream& os, const modint1000000007 &m){ os << m.val(); return os; } std::ostream &operator<<(std::ostream& os, const modint998244353 &m){ os << m.val(); return os; } template std::ostream &operator<<(std::ostream& os, const pair &p){ os << "(" << p.first << ", " << p.second << ")"; return os; } template std::ostream &operator<<(std::ostream& os, const vector &v){ int n = v.size(); os << "["; for(int i = 0; i < n; i++){ if(i == n - 1) os << v[i]; else os << v[i] << ", "; } os << "]"; return os; } template std::ostream &operator<<(std::ostream& os, const map &mp){ for(auto iter = mp.begin(); iter != mp.end(); iter++){ os << "(" << iter->first << ", " << iter->second << ")" << " "; } return os; } template std::ostream &operator<<(std::ostream& os, const set &s){ os << "{"; for(auto c : s) os << c << " "; os << "}"; return os; } #define LOCAL #ifdef LOCAL #define dump(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl #else #define dump(x) true #endif //using mint = modint1000000007; //using mint = modint998244353; void solve(){ ll n; cin >> n; vl a(n), b(n); rep(i,n){ cin >> a[i]; --a[i]; } rep(i,n){ cin >> b[i]; --b[i]; } fenwick_tree bit(1000000); rep(i, n){ bit.add(a[i], 1); } vl list; rep(i, n){ if(a[i] == b[i]){ list.pb(i); } else if(a[i] > b[i]){ if(bit.sum(b[i], a[i]) == 0){ list.pb(i); } } else { if(bit.sum(a[i], b[i]) == 0){ list.pb(i); } } } ll m = 0; vl al(n, 0); V> ord; for(ll i : list){ m += abs(a[i] - b[i]); al[i] = 1; bit.add(a[i], -1); bit.add(b[i], 1); if(a[i] > b[i]){ ord.push_back({'L', abs(a[i] - b[i])}); if(i < n - 1 && !al[i+1]){ list.pb(i+1); } } else if(b[i] > a[i] && !al[i-1]){ ord.push_back({'R', abs(a[i] - b[i])}); if(i > 0){ list.pb(i-1); } } } cout << m << '\n'; for(auto [c, x] : ord){ cout << c << ' ' << x << '\n'; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << std::fixed << std::setprecision(15); solve(); return 0; }