1 solutions
-
0
C :
#include "stdio.h" int n,m; int c,C[5]; int a[41][41][41][41]; int b[351]; int main() { int i,j,k,l,t; scanf("%d%d",&n,&m); for(i=0;i<n;i++) scanf("%d",&b[i]); memset(C,0,sizeof(C)); for(i=0;i<m;i++) { scanf("%d",&c); C[c]++; } a[0][0][0][0]=b[0]; for(i=0;i<=C[1];i++) for(j=0;j<=C[2];j++) for(k=0;k<=C[3];k++) for(l=0;l<=C[4];l++) { t=0; if(i>0&&t<a[i-1][j][k][l]) t=a[i-1][j][k][l]; if(j>0&&t<a[i][j-1][k][l]) t=a[i][j-1][k][l]; if(k>0&&t<a[i][j][k-1][l]) t=a[i][j][k-1][l]; if(l>0&&t<a[i][j][k][l-1]) t=a[i][j][k][l-1]; a[i][j][k][l]=t+b[i*1+j*2+k*3+l*4]; } printf("%d\n",a[C[1]][C[2]][C[3]][C[4]]); return 0; }
C++ :
#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N(42); int score[355],card[5]={0},f[N][N][N][N]={0}; //ifstream cin("tortoise.in"); //ofstream cout("tortoise.out"); int main() { int n,m; while (cin>>n>>m) { memset(card,0,sizeof(int)*5); memset(f,0,sizeof(int)*N*N*N*N); for (int i=1;i<=n;i++) cin>>score[i]; for (int i=1,x;i<=m;i++) { cin>>x; card[x]++; } f[0][0][0][0]=score[1]; for (int x1=0;x1<=card[1];x1++) for (int x2=0;x2<=card[2];x2++) for (int x3=0;x3<=card[3];x3++) for (int x4=0;x4<=card[4];x4++) { if (x1==0&&x2==0&&x3==0&&x4==0) continue; int k=1+x1+2*x2+3*x3+4*x4,t(0); if (x1>0) t=max(t,f[x1-1][x2][x3][x4]); if (x2>0) t=max(t,f[x1][x2-1][x3][x4]); if (x3>0) t=max(t,f[x1][x2][x3-1][x4]); if (x4>0) t=max(t,f[x1][x2][x3][x4-1]); f[x1][x2][x3][x4]=t+score[k]; } cout<<f[card[1]][card[2]][card[3]][card[4]]<<endl; } // system("pause"); return 0; }
Pascal :
program spc; var f:array[-1..40,-1..40,-1..40,-1..40] of longint; h:array[1..4] of integer; a:array[1..355] of integer; i,j,k,l,maxx:longint; n,m,t:integer; function max(a1,a2:longint):longint; begin if a1>a2 then exit(a1) else exit(a2); end; begin readln(n,m); for i:=1 to n do read(a[i]); for j:=1 to m do begin read(t); inc(h[t]); end; for i:=0 to h[1] do for j:=0 to h[2] do for k:=0 to h[3] do for l:=0 to h[4] do begin maxx:=max(f[i-1,j,k,l],f[i,j-1,k,l]); maxx:=max(maxx,f[i,j,k-1,l]); maxx:=max(maxx,f[i,j,k,l-1]); f[i,j,k,l]:=maxx+a[i+j*2+k*3+l*4+1]; end; writeln(f[h[1],h[2],h[3],h[4]]); end.
- 1
Information
- ID
- 733
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By