Unfair Poll

Description:

On the Literature lesson Sergei noticed an awful injustice, it seems that some students are asked more often than others.

Seating in the class looks like a rectangle, where n rows with m pupils in each.

The teacher asks pupils in the following order: at first, she asks all pupils from the first row in the order of their seating, then she continues to ask pupils from the next row. If the teacher asked the last row, then the direction of the poll changes, it means that she asks the previous row. The order of asking the rows looks as follows: the 1-st row, the 2-nd row, ..., the n - 1-st row, the n-th row, the n - 1-st row, ..., the 2-nd row, the 1-st row, the 2-nd row, ...

The order of asking of pupils on the same row is always the same: the 1-st pupil, the 2-nd pupil, ..., the m-th pupil.

During the lesson the teacher managed to ask exactly k questions from pupils in order described above. Sergei seats on the x-th row, on the y-th place in the row. Sergei decided to prove to the teacher that pupils are asked irregularly, help him count three values:

  1. the maximum number of questions a particular pupil is asked,
  2. the minimum number of questions a particular pupil is asked,
  3. how many times the teacher asked Sergei.

If there is only one row in the class, then the teacher always asks children from this row.

 

Input:

The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom.

The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen.

Output:

In the only line print the integer S — the minimum number of burles which are had to spend.

Examples:

Input

5
0 1 2 3 4

Output

10

Input

5
1 1 0 1 1

Output

1

Input

3
1 3 1

Output

4

Input

1
12

Output

0

Note

In the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.

In the second example it is enough to give one burle to the third citizen.

In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.

In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles.

 

思路:

1、首先观察到N,M并不大,那么我们考虑先处理掉提问次数剩余<N*M的时候,那么剩下的部分我们只要暴力涂提问次数即可。

 

2、我们发现提问一轮(从1走出去再回到1)是按照1.2.3.4....n.n-1.....2来的。

那么就是第一行和最后一行只涂了一次(提问了一次);

中间的所有行都涂了两次。

那么每一轮提问都需要涂:(2*n-2)*m次。

那么我们一共就涂了K/((2*n-2)*m)个整数次。

那么剩余部分就是K%((2*n-2)*m)。

剩余部分暴力涂上去即可。

查询也是暴力查询的。

 

3、注意一点,N==1的时候需要特判。

 

 

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
ll ans[150][150];
ll n,m,k,x,y;
int main()
{
    while(~scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&k,&x,&y))
    {
        if(n==1)
        {
            ll all=k/(n*m);
            ll yu=k%(n*m);
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    if(yu>=1)
                        ans[i][j]=all+1;
                    else ans[i][j]=all;
                    yu--;
                }
            }
            ll maxn=0;
            ll minn=2000000000000000000;
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    maxn=max(maxn,ans[i][j]);
                    minn=min(minn,ans[i][j]);
                }
            }
            printf("%I64d %I64d %I64d\n",maxn,minn,ans[x][y]);
        }
        else
        {
            ll all=k/((2*n-2)*m);
            ll yu=k%((2*n-2)*m);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(i==1||i==n)ans[i][j]=all;
                    else ans[i][j]=all*2;
                }
            }
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    if(yu>=1)
                    ans[i][j]++;
                    yu--;
                }
            }
            for(int i=n-1; i>=2; i--)
            {
                for(int j=1; j<=m; j++)
                {
                    if(yu>=1)
                    ans[i][j]++;
                    yu--;
                }
            }
            ll maxn=0;
            ll minn=2000000000000000000;
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    maxn=max(maxn,ans[i][j]);
                    minn=min(minn,ans[i][j]);
                }
            }
            printf("%I64d %I64d %I64d\n",maxn,minn,ans[x][y]);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值