二分买的产品的数量,排序,最小cost,简单题。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
#define maxn 100010
ll a[maxn];
ll c[maxn];
ll cost;
int n;
ll s;
ll check (ll mid){
int i;
//printf("mid =%d\n",mid);
if (mid > n)return 0;
for (i=1;i<=n;i++){
c[i] = a[i]+i*mid;
}
sort (c+1,c+1+n);
ll sum = 0;
for (i=1;i<=mid;i++){
sum += c[i];
if (sum >s) return 0;
//printf("c[%d]= %d",i,c[i]);
}
///printf("\n");
//printf("%d\n",sum);
cost = sum;
return 1;
}
int main (){
cin >> n>>s;
int i;
for (i=1;i<=n;i++){
cin >>a[i];
}
ll l = 0;
ll r = n;
ll mid;
ll ans =0;
while (r -l>=0){
mid = (r+l)/2;
ll res = check (mid);
if (res>0) {
l = mid+1;
ans = mid;
}
else r = mid-1;
}
cout <<ans <<' '<< cost <<endl;
}