跳到正文

剪贴板

剪贴板 is9uaopc

nbwnd
原文
剪贴板信息

剪贴板信息

发布
收录
更新
C++
#include<bits/stdc++.h>
//吹き込んだそよ風が
//窓辺の花を揺らして
//仰ぐ今日の空は
//あの時描いた青だった
namespace fast_IO {
#define IOSIZE 1000000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long 
//NO all in 
//NEVER get mad
//pay attention
//Clear all when start testing a new data

const int N=315;
const int inf=1e9;

int f[N][N][N];
int a[N],b[N];
int ln[N],rn[N];
int siz[N],sum[N];
int stk[N],top,tot;
int n,k,ans;

bool cmp(int x,int y)
{
	return x<y;
}

signed main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++) cin>>a[i],ans+=2*a[i];
	for(int i=1;i<=k;i++) cin>>b[i];
	for(int i=1;i<=n;i++)
	{
		bool fl=0;
		for(int j=1;j<=k;j++)
			if(b[j]==i) fl=1;
		if(!fl) stk[++top]=a[i];
	}
	sort(stk+1,stk+1+top,cmp);
	a[0]=a[n+1]=inf;
	ans+=2ll*inf;
	b[0]=0,b[++k]=n+1;
	for(int i=1;i<=k;i++)
	{
		if(b[i]==b[i-1]+1) 
			ans+=abs(a[b[i]]-a[b[i-1]]);
		else 
		{
			ln[++tot]=a[b[i-1]];
			rn[tot]=a[b[i]];
			siz[tot]=b[i]-b[i-1]-1;
			if(rn[tot]<ln[tot]) swap(ln[tot],rn[tot]);
		}
	}
	for(int sit=0;sit<(1<<tot);sit++)
		for(int i=1;i<=tot;i++)
			if((1<<(i-1))&sit)
				sum[sit]+=siz[i];
	memset(f,0x3f,sizeof f);
	for(int i=1;i<=top;i++) f[i+1][i][0]=0;
	for(int len=1;len<=top;len++)
		for(int l=1;l+len-1<=top;l++)
		{
			int r=l+len-1;
			for(int sit=0;sit<(1<<tot);sit++)
			{
				f[l][r][sit]=min(f[l][r][sit],min(f[l+1][r][sit],f[l][r-1][sit]));
				if(sum[sit]>len) continue;
				for(int t=(sit-1)&sit;t;t=(t-1)&sit)
					f[l][r][sit]=min(f[l][r][sit],f[l][l+sum[t]-1][t]+f[l+sum[t]][r][sit-t]);
				if(len==sum[sit])
					for(int i=1;i<=tot;i++)
						if(sit&(1<<(i-1)))
						{
							if(siz[i]==1&&len==1)
								f[l][r][sit]=min(f[l][r][sit],abs(stk[l]-ln[i])+abs(stk[l]-rn[i]));
							else if(siz[i]>1) 
								f[l][r][sit]=min(f[l][r][sit],f[l+1][r-1][sit^(1<<(i-1))]+abs(stk[l]-ln[i])+abs(stk[r]-rn[i])+stk[r]-stk[l]);
						}
			}
		}
	ans=ans+f[1][top][(1<<tot)-1];
	ans=ans/2ll;
	ans=ans-2ll*inf;
	cout<<ans<<endl;
	return 0;
}
//NO all in 
//NEVER get mad
//pay attention
//Clear all when start testing a new data