标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) const int N=1e3+10,M=1e6+1010,inf=1e9+10,mod=1e9+7; const ll INF=1e18+10; ll dp[N][N]; ll a[N]; ll b[N]; ll sum[N]; int main() { int T; scanf("%d",&T); while(T--) { memset(dp,0,sizeof(dp)); memset(sum,0,sizeof(sum)); int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); for(int i=1;i<=n;i++) scanf("%lld",&b[i]),sum[i]=sum[i-1]+b[i]; for(int i=2;i<=n;i++) { for(int t=1;t+i-1<=n;t++) { if(__gcd(a[t],a[t+i-1])!=1&&sum[t+i-2]-sum[t]==dp[t+1][t+i-2]) { dp[t][t+i-1]=sum[t+i-1]-sum[t-1]; continue; } for(int j=t;j<=t+i-2;j++) { dp[t][t+i-1]=max(dp[t][t+i-1],dp[t][j]+dp[j+1][t+i-1]); } } } printf("%lld\n",dp[1][n]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5893985.html