Robberies
For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.
His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
InputThe first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj .
Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .OutputFor each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.
Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.Sample Input
3 0.04 3 1 0.02 2 0.03 3 0.05 0.06 3 2 0.03 2 0.03 3 0.05 0.10 3 1 0.03 2 0.02 3 0.05
Sample Output
2 4 6
Sponsor
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #include<set> 5 #include<iostream> 6 #include<map> 7 #include<stack> 8 #include<cmath> 9 #include<algorithm> 10 #define ll long long 11 using namespace std; 12 int n,k; 13 int f[4][2]={1,0,0,1,-1,0,0,-1}; 14 int dp[1000][1000],e[1000][1000]; 15 int dfs(int x,int y) 16 { 17 if(dp[x][y]) return dp[x][y]; 18 for(int i=0;i<4;i++) 19 { 20 for(int j=1;j<=k;j++) 21 { 22 int tx=x+j*f[i][0]; 23 int ty=y+j*f[i][1]; 24 if(tx>=0&&ty>=0&&tx<n&&ty<n&&e[tx][ty]>e[x][y]) 25 { 26 dp[x][y]=max(dfs(tx,ty)+e[tx][ty],dp[x][y]); 27 } 28 } 29 } 30 return dp[x][y]; 31 } 32 int main() 33 { 34 while(~scanf("%d%d",&n,&k)&&(n+k)!=-2) 35 { 36 memset(dp,0,sizeof(dp)); 37 for(int i=0;i<n;i++) 38 for(int j=0;j<n;j++) 39 scanf("%d",&e[i][j]); 40 printf("%d\n",dfs(0,0)+e[0][0]); 41 } 42 }