fork download
  1. #include<stdio.h>
  2. int check(int time,int rank[],int n,int prata) //for given time find max prata
  3. {
  4. int max[8];
  5. int sum=0;
  6. for(int i=1;i<=8;i++)
  7. {
  8. int temp=0; //time total
  9. int x=0; //total prata
  10. for(int j=1;;j++)
  11. {
  12.  
  13. temp+=(j*i);
  14. if(temp>time)
  15. break;
  16. x++;
  17. }
  18. max[i-1]=x;
  19. }
  20.  
  21. //for(int i=0;i<8;i++)
  22. // printf("%d\t",max[i]);
  23. //printf("\n");
  24.  
  25. for(int i=0;i<n;i++)
  26. sum=sum+max[rank[i]-1];
  27. //printf("Sum = %d\n",sum);
  28. if(sum>=prata)
  29. return 1;
  30. return 0;
  31. }
  32. int main()
  33. {
  34. int test,p,l;
  35. //int arr[]={1,1,1,1,1,1,1,1};
  36. //check(1,arr,8,8);
  37. scanf("%d",&test);
  38. while(test--)
  39. {
  40. scanf("%d%d",&p,&l);
  41. int arr[l];
  42. for(int i=0;i<l;i++)
  43. scanf("%d",&arr[i]);
  44. int low=1,high=99999999;
  45. while(low<high-1) //To check later
  46. {
  47. //printf("HI");
  48. int mid=(low+high)/2;
  49. if(check(mid,arr,l,p))
  50. high=mid;
  51. else
  52. low=mid; //To check
  53. }
  54. printf("%d\n",high);
  55. }
  56. return 0;
  57. }
Success #stdin #stdout 0s 2056KB
stdin
3
10
4 1 2 3 4
8
1 1
8
8 1 1 1 1 1 1 1 1
stdout
12
36
2