Question

In: Computer Science

Jojo the wizard is currently on a dragon’s lair. To escape from the lair, Jojo needs...

Jojo the wizard is currently on a dragon’s lair. To escape from the lair, Jojo needs to defeat the elder dragon. Jojo is currently a wizard under training, so he knows only one spell : the fireball spell. The dragon’s body can be represented as an array of size N , where each part of the dragon’s body, from its head to its tail, is numbered with integer from 1 to N. Each body part has its sensitivity to fire a i . If a i is positive, then the fireball spell will deal a i damage to the dragon. If a i is negative, it will heal the dragon by − a i health points instead.

The fireball spell is a spell which deals damage to the dragon’s body on exactly K consecutive body parts. That is, if Jojo casts the spell on i-th body part, then the fireball will attack the parts [i, i+1, i+2, ..., i + K −1]. For example, if the dragon’s body is [1 , 2 , − 2 , 4 , 5], and K is 3, then Jojo may cast the spell on 1st, 2nd, and 3rd body part, which deals 1, 4, and 7 damage respectively. Jojo can’t cast the spell on 4 or 5 since the fireball must hit exactly K body parts of the dragon. As Jojo can only cast the spell one time, Help Jojo determine the maximum damage output he can deal to the dragon.

Format Input

The first line of the input contains two integers N and K, the number of the dragon’s body part and the range of the fireball spell. The next line consists of N integers a i , the sensitivity of each body part.

Format Output

Output the maximum damage Jojo can deal to the elder dragon. If Jojo can’t deal any damage, output 0 instead.

Constraints

• 1 ≤ K ≤ N ≤ 103

• 0 ≤ | a i | ≤ 109

Sample Input 1 (standard input)

5 3

1 2 -2 4 5

Sample Output 1 (standard output)

7

Sample Input 2 (standard input)

5 1

-1 -2 -3 -4 -5

Sample Output 2 (standard output)

0

Explanation

On the second sample, no matter where Jojo choose to cast his fireball spell, it will heal the dragon. So, the answer to the second sample is 0.

NOTE: USE C LANGUAGE, DONT USE FUNCTION(RESULT,RETURN),VOID,RECURSIVE, USE BASIC CODE AND CODE IT UNDER int main (){, constraint must be the same

Solutions

Expert Solution

Please find your solution below and if doubt comment and do upvote.

CODE:

#include <stdio.h>

int main()
{
    int n,k;
    //take user input
    scanf("%d",&n);
    scanf("%d",&k);
    //make an array since N is of order 10^3
    //long int array is becoz a[i] is of order 10^9
    long int arr[10000];
    //take user input
    for(int i=0;i<n;i++)
    {
        scanf("%ld",&arr[i]);
    }
    //to store maximum damage
    long int Maxsum=0;
    //traverse till n-k index in first loop
    // consider every k size subarray and sum them
    //then compare and find the Maxsum
    for(int i=0;i<n-k+1;i++)
    {
        long int localMax=0;
        //add the subarray of size k
        for(int j=i;j<k+i;j++)
        {
            localMax+=arr[j];
        }
       //compare and find Maxsum
        if(Maxsum<localMax)
        Maxsum=localMax;
    }
    //print the output
    printf("%ld",Maxsum);

    return 0;
}

OUTPUT:


Related Solutions

BROKEN SPEEDOMETER The cycling competition is near, so jojo needs to practice cycling. he used a...
BROKEN SPEEDOMETER The cycling competition is near, so jojo needs to practice cycling. he used a speedometer found in his basement to measure his speed. however, it turns out that the speedometer is not usual! the digits on the speedometer can only count up to 2. if we add a value to a digit 2, it will revert to 0 and adds a value to the next digit. for example, the numbers [0,1,2,3,4,5,6,7] will be shown as [0,1,2,10,11,12,20,21] on the...
Cambridge Ltd is currently suffering from a depression in the market for its products and needs...
Cambridge Ltd is currently suffering from a depression in the market for its products and needs to decide which of three products it should continue produce, so that it can maximise its profits. They have recently employed the services of Research It Ltd who have researched Cambridge Ltd and their products and have produced the following information: Research It Ltd. have provided information for three levels of demand, low, medium and high and a probabilities for each level of demand...
Wizard Inc. has to choose between two mutually exclusive projects. If it chooses project A, Wizard...
Wizard Inc. has to choose between two mutually exclusive projects. If it chooses project A, Wizard Inc. will have the opportunity to make a similar investment in three years. However, if it chooses project B, it will not have the opportunity to make a second investment. The following table lists the cash flows for these projects. If the firm uses the replacement chain (common life) approach, what will be the difference between the net present value (NPV) of project A...
What is the value of escape velocity for a body to escape from.earth gravitational field
Explain escape velocity details. What is the value of escape velocity for a body to escape from earth gravitational field 
In an attempt to escape from a deserted island, a castaway builds a raft and sets...
In an attempt to escape from a deserted island, a castaway builds a raft and sets sail for the sea. The wind changes a lot during the day and flies along the following straight lines: 2.60 km and 45.0 ° north of west, then 4.80 km and 60.0 ° south of east, then 1.60 km and 5.00 ° east of north, and finally 2.70 km and 13.0 ° north of the east. Use the analytical method to find the resulting...
The graph represents the market for Wizard and Witch wands, a market currently captured by the monopolist, Ollivander. Use this graph to answer the proceeding questions.
The graph represents the market for Wizard and Witch wands, a market currently captured by the monopolist, Ollivander. Use this graph to answer the proceeding questions.At what price does Ollivander sell his wands for, and how many units does he sell?
Calculate the amount of energy required to escape from the surface of the following bodies, relative...
Calculate the amount of energy required to escape from the surface of the following bodies, relative to that required to escape from the surface of Earth. (a)    Uranus                      energy to escape Earth (b)    Mars                              energy to escape Earth
Roads A, B, and C are the only way to escape from a certain provincial prison....
Roads A, B, and C are the only way to escape from a certain provincial prison. Prison records show that, of the prisoners who tried to escape, 9 % used road A, 14 % used road B, the remainder used road C. The records also indicate that 85 % of those who tried to escape using road A were captured. 13 % of those using road B were captured, and 58 % of those using road C were captured. Use...
Active Directory Domain Services Installation Wizard
Active Directory Domain Services Installation Wizard
Determine the escape velocity of a projectile fired from the Earth’s Surface, using a differential equation....
Determine the escape velocity of a projectile fired from the Earth’s Surface, using a differential equation. Compare this with the escape velocity of the other planets, including Pluto.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT