Codeforces J. Sagheer and Nubian Market

Subject description:

Sagheer and Nubian Market

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost \(a_i\) Egyptian pounds. If Sagheer buys k items with indices x1, x2, ..., \(x_k\), then the cost of item *x**j* is \(a_{x_j}\) + \(x_j\)·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.

Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?

Input

The first line contains two integers n and S (1 ≤ n ≤ 105 and 1 ≤ S ≤ 109) — the number of souvenirs in the market and Sagheer's budget.

The second line contains n space-separated integers a1, a2, ..., *a**n* (1 ≤ *a**i* ≤ 105) — the base costs of the souvenirs.

Output

On a single line, print two integers k, T — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy these k souvenirs.

Examples

Input

Copy

3 112 3 5

Output

Copy

2 11

Input

Copy

4 1001 2 5 6

Output

Copy

4 54

Input

Copy

1 77

Output

Copy

0 0

Note

In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.

In the second example, he can buy all items as they will cost him [5, 10, 17, 22].

In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.

Ideas:

The title is said that there are S dollars, there are N items, the price of goods is the original price plus the number of items purchased multiplied by item number, each item can only buy once. S seeking to buy the maximum number of items, if the amount calculated under the same conditions with the least money to buy.

At first, a look that n \ (10 ^ 5 \) in the case of the order, you can enumerate do, from 0 to n, each enumerate all count cost of each item, in order to buy most of the items to spend as little as possible, to take a row sequence every kind of goods, and then seek a prefix cost, then lower_bound binary search position is greater than the value of S, minus one is less S takes a position referred to as pos.

The following detailed talk about the meaning of pos: If a position pos (prefix number of items here and the location is actually purchased) found to spend less S, the position pos expressed under sufficient funds, the enumeration lower limit of the number of items i, you can buy a maximum of two items pos. If pos equals i, just right is the maximum number of items can be purchased. If pos> i, because now might not run out S, it means that it is possible to buy more goods (simultaneously buying more items each item of spending is higher), if pos <i, it means that now funds use the maximum extent i can not buy one item, indicating that i do not satisfy the conditions to continue the enumeration down. (This was the thought out of consciousness is not clear, for now the feeling is not even well understood, admire my own).

Because i is descending enumeration, that pos> possibility to buy more state i represents the enumeration has passed, there are only two possibilities, it is the current enumeration values meet and satisfied. Once satisfied appear out of the loop, the output answer. The time complexity of this method is the \ (O (n ^ 2log_2n) \) , and not too many data.

Code:

#include <iostream>
#include <algorithm>
#include <memory.h>
#include <cstdio>
#define max_n 100005
#define INF 0x3f3f3f3f
using namespace std;
int n;
int S;
int a[max_n];
long long sum[max_n];
template<typename T>
inline void read(T& x)
{
    x=0;int f=0;char ch=getchar();
    while('0'>ch||ch>'9'){if(ch=='-')f=1;ch=getchar();}
    while('0'<=ch&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    x=f?-x:x;
}
#pragma optimize(2)
int main()
{
    read(n);
    read(S);
    int minm = INF;
    for(int i = 1;i<=n;i++)
    {
        read(a[i]);
        minm = min(minm,a[i]);
    }
    int items = S/minm;//降一下枚举的起点
    items = min(items,n);
    long long money = 0;
    int pos = 0;
    for(int i = items;i>0;i--)
    {
        memset(sum,0,sizeof(sum));
        for(int j = 1;j<=n;j++)
        {
            sum[j] = a[j] + i*j;//求花费
        }
        sort(sum+1,sum+n+1);
        /*for(int j = 1;j<=n;j++)
        {
            cout << sum[j] << " ";
        }
        cout << endl;*/
        for(int j = 1;j<=n;j++)
        {
            sum[j] += sum[j-1];//求花费的前缀和
        }
        /*for(int j = 1;j<=n;j++)
        {
            cout << sum[j] << " ";
        }
        cout << endl;
        cout << endl;*/
        pos = upper_bound(sum+1,sum+n+1,S)-sum-1;
        //cout << "pos " << pos << endl;
        if(pos>=i)
        {

            if(pos<=n)//这里是所有元素都小于S时的情况
            {
                money = sum[pos];
            }
            else
            {
                money = sum[pos-1];
            }
            break;
        }
    }
    printf("%d %I64d\n",pos,money);
    return 0;
}

In fact, the enumeration is right, but not linear enumeration, enumeration bipartite, complexity down to \ (O (log_2 (nlog_2n)) \) .

Note that returns the number of items to meet the conditions enumerated last record under.

Code:

#include <iostream>
#include <algorithm>
#define max_n 100005
using namespace std;
int n;
int S;
int a[max_n];
long long sum[max_n];
long long cost = 0;
long long mincost = 0;
int items = 0;
template<typename T>
inline void read(T& x)
{
    x=0;int f=0;char ch=getchar();
    while('0'>ch||ch>'9'){if(ch=='-')f=1;ch=getchar();}
    while('0'<=ch&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    x=f?-x:x;
}
#pragma optimize(2)
int main()
{
    read(n);
    read(S);
    for(int i = 1;i<=n;i++)
    {
        read(a[i]);
    }
    int l = 0;
    int r = n;
    int mid = 0;
    while(l<=r)
    {
        //cout << "l " << l << " r " << r << endl;
        cost = 0;
        mid = (l+r)>>1;
        //cout << "mid " << mid << endl;
        for(int i = 1;i<=n;i++)
        {
            sum[i] = (long long)a[i]+(long long)mid*i;
        }
        sort(sum+1,sum+n+1);
        for(int i = 1;i<=mid;i++)
        {
            cost += sum[i];
        }
        //cout << "cost " << cost << endl;
        if(S>=cost)
        {
            items = mid;
            mincost = cost;
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    printf("%d %I64d",items,mincost);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zhanhonhao/p/11345562.html