/* Exact maximization over the circular key and all optional printed-F holds.
   stdin tasks: alphabet n; n ciphertext; n reward flags; alphabet target flags. */
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
struct Segment {int H[4][29]={};int rot=0, f=-1;};
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int A,n;
 while(cin>>A>>n){
  if(A<2||A>29||n<1)return 2;
  vector<int>x(n),use(n),T(A);for(int&v:x)cin>>v;for(int&v:use)cin>>v;for(int&v:T)cin>>v;
  vector<Segment> blocks(1);
  for(int i=0;i<n;i++){
   auto &b=blocks.back();
   if(x[i]==0){b.f=use[i];blocks.emplace_back();continue;}
   if(use[i])for(int k=0;k<A;k++)b.H[b.rot][k]+=T[(x[i]+A-k)%A];
   b.rot=(b.rot+1)%4;
  }
  int top=-1;array<int,4> best{};long long checked=0;
  for(int a=0;a<A;a++)for(int b=a;b<A;b++)for(int c=a;c<A;c++)for(int e=a;e<A;e++){
   array<int,4> key{a,b,c,e};
   bool rep=false;
   for(int q=1;q<4;q++){
    array<int,4> alt=key;rotate(alt.begin(),alt.begin()+q,alt.end());
    if(alt<key){rep=true;break;}
   }
   if(rep)continue;
   checked++;
   int k[]={a,b,c,e,a,b,c,e},zero[4];for(int i=0;i<4;i++)zero[i]=T[(A-k[i])%A];
   int d[4]={0,0,0,0},u[4];
   for(auto&g:blocks){
    for(int s=0;s<4;s++)u[(s+g.rot)&3]=d[s]+g.H[0][k[s]]+g.H[1][k[s+1]]+g.H[2][k[s+2]]+g.H[3][k[s+3]];
    if(g.f<0)break;
    for(int s=0;s<4;s++)d[s]=max(u[s]+g.f*T[0],u[(s+3)&3]+g.f*zero[(s+3)&3]);
   }
   int v=*max_element(u,u+4);if(v>top){top=v;best=key;}
  }
  long long expected=((long long)A*A*A*A+A*A+2*A)/4;if(checked!=expected)return 3;
  cout<<top;for(int k:best)cout<<' '<<k;cout<<' '<<checked<<'\n'<<flush;
 }
}
