#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=2e5+5,inf=1e18,minf=-1e18,lg=30;
//#undef int
int n,k,m,w,h,l;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int mid;
int dp[mxn+10],from[mxn+10],di[mxn+10],d[mxn+10];
vector<int>F,S;
//max prefix flower we can get
//
bool intersect(pii a,pii b){
return !((a.f>b.s)||(b.f>a.s));
}
pii merge(pii a,pii b){
return {min(a.f,b.f),max(a.s,b.s)};
}
int getR(int x){
int a=ub(all(F),x)-F.begin();
//cout<<a<<"YES\n";
//if(F[a]<=x)a++;
return a-1;
}
int getL(int x){return lb(all(F),x)-F.begin()-1;}
int getrange(int l,int r){
return getR(r)-getL(l);
}
bool check(){
for(int i=1;i<=n;i++)dp[i]=0;
for(int i=1;i<=n;i++){
int l=i;
for(int j=i;j>max(0LL,i-3);j--)if(S[i]-S[j]<=mid)l=j;
int mn=F[dp[l-1]+1];
from[i]=l;
for(int k=0;k<(1LL<<(i-l+1));k++){
vector<pii>range;
int mx=0;
for(int j=0;j<i-l+1;j++){
pii cr;
if(k&(1LL<<j))cr={S[i-j],S[i-j]+mid};
else cr={max(0LL,S[i-j]-mid),S[i-j]};
mx=max(mx,cr.s);
int yes=1;
for(auto &j:range)if(intersect(j,cr)){
j=merge(j,cr);
yes=0;
}
if(yes)range.pb(cr);
}
if(range.size()>1&&intersect(range[0],range[1])){
pii a=merge(range[0],range[1]);
range.clear();
range.pb(a);
}
int need=getrange(mn,mx),cnt=0;
for(auto j:range)cnt+=getrange(j.f,j.s);
if(cnt==need){
int x=getR(mx);
if(dp[i]<x)dp[i]=x,d[i]=k;
}
}
if(dp[i]==m){
int cur=i;
while(cur>0){
int c=0;
for(int j=from[cur];j<=cur;j++)di[j]=(d[cur]&(1LL<<(cur-j)));
cur=from[cur]-1;
}
return 1;
}
}
return 0;
}
int32_t main(){
fastio
cin>>n>>m;
S.resize(n+1,-1);
F.resize(m+1,-1);
for(int i=1;i<=n;i++)cin>>S[i];
for(int i=1;i<=m;i++)cin>>F[i];
int l=0,r=1e9,ans=inf;
while(l<=r){
mid=l+(r-l)/2;
if(check())r=mid-1,ans=min(ans,mid);
else l=mid+1;
}
if(ans==inf)return (cout<<-1),0;
cout<<ans<<'\n';
mid=ans;
check();
for(int i=1;i<=n;i++)cout<<((di[i])?'R':'L');
}
/*
*/
Compilation message (stderr)
Main.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
Main.cpp:38:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
38 | void setIO(string name){
| ^
Main.cpp:48:27: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
48 | bool intersect(pii a,pii b){
| ^
Main.cpp:51:22: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
51 | pii merge(pii a,pii b){
| ^
Main.cpp:54:15: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
54 | int getR(int x){
| ^
Main.cpp:60:15: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
60 | int getL(int x){return lb(all(F),x)-F.begin()-1;}
| ^
Main.cpp:61:25: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
61 | int getrange(int l,int r){
| ^
Main.cpp:64:12: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
64 | bool check(){
| ^
Main.cpp:110:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
110 | int32_t main(){
| ^
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |