Pagini recente » Cod sursa (job #479021) | Cod sursa (job #3273507) | Cod sursa (job #1169324) | Cod sursa (job #2523257) | Cod sursa (job #2846332)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("tricouri.in");
ofstream fout("tricouri.out");
long long n,dp[10][25],i,t,x,j,i2,j2,val,a,b;
int main()
{
fin>>n>>t;
for(i=1;i<=n;i++){
fin>>x;
for(i2=1;i2<=6;i2++){
for(j=1;j<=21;j++){
if(dp[i2][j]){
val = x + dp[i2][j];
for(j2=1;j2<=21;j2++){
if(val % j2 == 0){
dp[i2+1][j2] = max(dp[i2+1][j2],dp[i2][j]+x);
}
}
}
}
}
for(j=1;j<=21;j++){
if(x % j == 0){
dp[1][j] = max(dp[1][j],x);
}
}
}
while(t--){
fin>>a>>b;
if(!dp[a][b])
fout << -1 << '\n';
else
fout << dp[a][b] << '\n';
}
return 0;
}