fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. vector<int> a;
  25.  
  26. int getCost(int n, int k){
  27. vector<int> cost(n);
  28. for(int i=0; i<n; i++){
  29. cost[i] = a[i] + (i+1)*k; // 1 based indexing
  30. }
  31.  
  32. sort(begin(cost), end(cost));
  33.  
  34. int totalCost = 0;
  35. for(int i=0; i<k; i++){
  36. totalCost += cost[i];
  37. }
  38.  
  39. return totalCost;
  40. }
  41.  
  42. bool isPossible(int n, int S, int mid){
  43. vector<int> cost(n);
  44. for(int i=0; i<n; i++){
  45. cost[i] = a[i] + (i+1)*mid;
  46. }
  47.  
  48. sort(begin(cost), end(cost));
  49. int totalCost = 0;
  50. for(int i=0; i<mid; i++){
  51. totalCost += cost[i];
  52. }
  53. return totalCost <= S;
  54. }
  55.  
  56. pair<int,int> consistency(int n, int S){
  57.  
  58. int s = 0, e = n;
  59. while(s<e){
  60. int mid = s + (e-s+1)/2;
  61. if(isPossible(n, S, mid)){
  62. s = mid;
  63. }
  64. else e = mid-1;
  65. }
  66.  
  67. return {e, getCost(n, e)};
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. void solve() {
  79.  
  80. int n, S;
  81. cin>>n >> S;
  82.  
  83. a.resize(n);
  84. for(int i=0; i<n; i++) cin >> a[i];
  85.  
  86. auto ans1 = consistency(n, S);
  87.  
  88. cout << ans1.first << " " << ans1.second << endl;
  89.  
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. int32_t main() {
  97. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  98.  
  99. int t = 1;
  100. // cin >> t;
  101. while (t--) {
  102. solve();
  103. }
  104.  
  105. return 0;
  106. }
Success #stdin #stdout 0.01s 5324KB
stdin
3 11
2 3 5
4 100
1 2 5 6
1 7
7
stdout
2 11
4 54
0 0