fork download
  1. //@auther hussein zayed
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int n;
  7. vector<long long> c(8,1000003); // arrC to save count A,B,C,AB,AC,CB,ABC but put price it ;
  8. vector<long long>arr; //arr to save price sum index with together from arrC
  9. //then do ABC
  10.  
  11. int main()
  12. {
  13. string s,S="";
  14. int a=0,b=0,C=0;
  15. ios::sync_with_stdio(0);
  16. cin >> n;
  17.  
  18. for(int i = 0; i < n; i++)
  19. {
  20. int x;
  21.  
  22. cin >> x >> s;
  23. S+=s;
  24. // arrC.......
  25. if(s == "A"){
  26. if(c[0]>x)
  27. c[0]=x;}
  28. else if(s == "B"){
  29. if(c[1]>x)
  30. c[1]=x;}
  31. else if(s == "C"){
  32. if(c[2]>x)
  33. c[2]=x;}
  34. else if(s == "AB" || s == "BA"){
  35. if(c[3]>x)
  36. c[3]=x;}
  37. else if(s == "BC" || s == "CB"){
  38. if(c[4]>x)
  39. c[4]=x;}
  40. else if(s == "AC" || s == "CA"){
  41. if(c[5]>x)
  42. c[5]=x;}
  43. else{
  44. if(c[6]>x)
  45. c[6]=x;}
  46. }
  47. //if dont found A or B or C print -1;
  48. for(int i=0;i<S.size();++i){
  49. if(S[i]=='A')
  50. a++;
  51. else if(S[i]=='B')
  52. b++;
  53. else
  54. C++;
  55. }
  56.  
  57. if(a==0||b==0||C==0){
  58. cout<<a;
  59. cout<<-1;
  60. return 0;
  61. }
  62. // arr price then found now
  63. if(c[0]!=0&&c[1]!=0&&c[2]!=0)
  64. arr.push_back(c[0]+c[1]+c[2]);
  65. if(c[0]!=0&&c[4]!=0)
  66. arr.push_back(c[0]+c[4]);
  67. if(c[1]!=0&&c[5]!=0)
  68. arr.push_back(c[1]+c[5]);
  69. if(c[2]!=0&&c[3]!=0)
  70. arr.push_back(c[2]+c[3]);
  71. if(c[3]!=0&&c[4]!=0)
  72. arr.push_back(c[3]+c[4]);
  73. if(c[3]!=0&&c[5]!=0)
  74. arr.push_back(c[3]+c[5]);
  75. if(c[4]!=0&&c[5]!=0)
  76. arr.push_back(c[4]+c[5]);
  77. if(c[6]!=0)
  78. arr.push_back(c[6]);
  79.  
  80. sort(arr.begin(),arr.end());
  81. cout<<arr[0];
  82. }
Success #stdin #stdout 0s 15240KB
stdin
4
5 C
6 B
16 BAC
4 A
stdout
15