The Enchanted Forest

Marisa comes to pick mushrooms in the Enchanted Forest.

The Enchanted forest can be represented by nn points on the XX-axis numbered 11 through nn. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by a1,a2,…,ana1,a2,…,an.

Marisa can start out at any point in the forest on minute 00. Each minute, the followings happen in order:

  • She moves from point xx to yy (|x−y|≤1|x−y|≤1, possibly y=xy=x).
  • She collects all mushrooms on point yy.
  • A new mushroom appears on each point in the forest.

Note that she cannot collect mushrooms on minute 00.

Now, Marisa wants to know the maximum number of mushrooms she can pick after kk minutes.

Input

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn, kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤1091≤k≤109) — the number of positions with mushrooms and the time Marisa has, respectively.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial number of mushrooms on point 1,2,…,n1,2,…,n.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print the maximum number of mushrooms Marisa can pick after kk minutes.

Example

input

Copy

4
5 2
5 6 1 2 3
5 7
5 6 1 2 3
1 2
999999
5 70000
1000000000 1000000000 1000000000 1000000000 1000000000

output

Copy

12
37
1000000
5000349985

Note

Test case 1:

Marisa can start at x=2x=2. In the first minute, she moves to x=1x=1 and collect 55 mushrooms. The number of mushrooms will be [1,7,2,3,4][1,7,2,3,4]. In the second minute, she moves to x=2x=2 and collects 77 mushrooms. The numbers of mushrooms will be [2,1,3,4,5][2,1,3,4,5]. After 22 minutes, Marisa collects 1212 mushrooms.

It can be shown that it is impossible to collect more than 1212 mushrooms.

Test case 2:

This is one of her possible moving path:

2→3→2→1→2→3→4→52→3→2→1→2→3→4→5

It can be shown that it is impossible to collect more than 3737 mushrooms.

思路:题目大致是说让一个人去割菜,一割会全割完。但是这里需要注意的是当割过后才会长1长度,不是割前就长1,这一点非常重要,会影响之后的等差数列求和的首项大小和末项大小。现在开始想如何收割呢?首先是与n和k的大小关系有关:

当k小于n时:这个好想,简单,我们只要选出连续的几个值之和最大即可,增长不会产生影响,因为时间太少选哪几个增长都是一样的,唯一有区别的是选本来就多的,这样就能割的更多。

当k大于n时:这个问题难,但是首先可以知道的是我们要全收割,因为本来有的话一定比不收割它值为0要大,因为有的话最小是1,也会比0大。然后难点就来了我们如何处理增长值呢?因为增长是全体的,我们可以考虑真正的增长速率。如果我们让时间刚好到时刚好遍历收割完一遍,那么增长速率就是n,即全部。如果当时间到时我们的位置在除端点外的任意位置的话,增长速率的总体就会减少,因为有几个在增长时没有算上,可以拿样例2来举例子,假设从时间刚开始时就从左端出发,跑到最右端后继续拐回来,走两步后停下来,时间到了,那么前的数增长的就没算上,(这里要明白不管最后到端点或者任意位置,总会必定舍弃有n*(n+1)/2个菜,因为跑过后还在增长。那么全部菜的增长就是n*k,可以用 总共的 - 舍弃的 = 得到的)假设我们在端点一直等到剩n的时间才走,那么既可以有全部的菜,又可以得到增长的,增长的可以用等差数列求和公式:n*(n+1)/2,这里说明一下求和公式的答案一定是整数,因为n与n+1必定有一个是偶数能整除2。这里注意得到的数是(从0开始的(是增长))k-n开始的,k-1结束的求和即可。

连续数组的最大值求法:先求前缀和,然后用前缀和数组sum减去前x个即可

for(int i=1;i<=n;i++)
{
    cin>>a[i];
    sum[i]=sum[i-1]+a[i];//求前缀和
}

for(int i=k;i<=n;i++)
{
    maxx=max(maxx,sum[i]-sum[i-k]);//得到最大连续子段
}

完整代码:

#include <bits/stdc++.h>

using namespace std;

#define int long long
const int mod=1e9+7;

const int N=2e5+10;
int a[N];
int sum[N];

void solve()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }

    if(k<n)
    {
        int maxx=0;
        for(int i=k;i<=n;i++)
        {
            maxx=max(maxx,sum[i]-sum[i-k]);
        }
        //int other=k*k-k*(k+1)/2;//割完随时长,不是割前就长
        int other=k*(k-1)/2;
        cout<<maxx+other<<endl;
    }
    else
    {
        int maxx=sum[n];
        //int other=n*k-(n+1)*n/2;
        int other=n*(k-n+k-1)/2;
        cout<<maxx+other<<endl;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Fabric Loader是Minecraft的一个模组加载器,使用它可以加载一些自定义的模组来扩展游戏的功能。如果要让穿戴有“附魔保护”的鞋子的玩家在游戏中不受到坠落伤害,可以编写以下格式的Fabric Loader模组代码: ``` package net.example.mod; import net.minecraft.entity.Entity; import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; import net.minecraft.world.World; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.player.AttackEntityCallback; public class ExampleMod implements ModInitializer { @Override public void onInitialize() { // Register the callback function for player attacks AttackEntityCallback.EVENT.register((PlayerEntity player, World world, Entity entity, int i, int j) -> { // Check if the player is wearing enchanted boots ItemStack boots = player.getEquippedStack(EquipmentSlot.FEET); if (boots.getItem() == Items.DIAMOND_BOOTS && boots.hasEnchant(Enchantments.FEATHER_FALLING)) { // Cancel fall damage entity.damage(DamageSource.FALL, 0f); } return ActionResult.PASS; }); } } ``` 这段代码注册了一个回调函数,当玩家攻击实体时会执行。在回调函数中,首先获取玩家当前穿戴的鞋子,然后检查鞋子是否为钻石鞋子,并且是否附有“附魔保护”的附魔。如果满足条件,则取消实体的坠落伤害。 因此,如果一个玩家在游戏中穿戴有这样的鞋子,那么即使他从高处掉落,也不会受到坠落伤害,可以起到保护作用。 ### 回答2: 要写一个Fabric Loader格式的Minecraft模组,使得穿戴带有附魔"摔落保护"的靴子的玩家不会踩坏耕地,可以按照以下步骤进行: 1. 先确定你已经安装了Fabric Loader和Fabric API,并在开发环境中设置好相关的文件结构和配置。 2. 创建一个新的Java类,命名为"EnchantmentProtectionMod"(可以根据个人喜好进行命名)。 3. 导入必要的类和接口,例如:`net.fabricmc.api.ModInitializer`和`net.fabricmc.fabric.api.event.player.BlockBreakEvents`。 4. 实现ModInitializer接口,并重写其中的`onInitialize`方法。 5. 在onInitialize方法中,注册一个监听器,监听玩家破坏方块的事件。 6. 在监听器的回调方法中,判断玩家所穿戴的靴子是否含有"摔落保护"附魔。 7. 如果玩家穿戴的靴子含有"摔落保护"附魔,判断即将被破坏的方块是否为耕地。 8. 如果即将被破坏的方块是耕地,取消方块破坏事件的监听,以避免玩家踩坏耕地。 9. 在完成代码编写后,进行编译并将生成的mod文件放入Minecraft的mod文件夹。 10. 启动Minecraft游戏,确保Fabric Loader正确加载并启用了你的mod。 11. 穿戴带有"摔落保护"附魔的靴子,并尝试在耕地上行走,确保不再踩坏耕地。 这是一个简单的实现示例,仅供参考。根据个人的需求和理解,可以进一步完善和优化这个mod。 ### 回答3: 要写一个Fabric Loader格式的Minecraft模组,使得穿戴带有附魔“摔落保护”的靴子的玩家不会踩坏耕地,可以按照以下步骤进行: 1. 首先,在你的开发环境中安装Fabric Loader和Minecraft开发工具包。 2. 创建一个新的Fabric mod项目,并编写mod的基本代码框架。 3. 定义一个新的附魔类型“耕地保护”,该附魔可以添加到靴子上。 4. 在靴子类中添加一个判断玩家是否穿着带有“耕地保护”附魔的靴子的方法。 5. 当玩家在踩踏方块时,通过这个方法判断玩家是否穿戴了带有“耕地保护”附魔的靴子。 6. 如果玩家穿戴了这样的靴子,则阻止玩家的踩踏行为对耕地方块造成破坏。 7. 添加其他必要的代码,如注册附魔、添加配置选项等。 8. 构建并测试你的模组,确保功能正常运作。 9. 发布你的模组,并与其他玩家共享。 通过上述步骤,你可以编写一个满足需求的Fabric Loader格式的Minecraft模组,使得穿戴带有附魔“摔落保护”的靴子的玩家不会踩坏耕地。模组的功能将增加游玩的便利性和可玩性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值