Give the Code in Python and as short as possible and simple to understand To protect people from evil, a long and tall wall was constructed a few years ago. But just a wall is not safe, there should also be soldiers on it, always keeping vigil. The wall is very long and connects the left and the right towers. There are exactly N spots (numbered 1 to N) on the wall for soldiers. The Kh spot is K miles far from the left tower and (N+1-K) miles from the right tower. Given a permutation of spots P of (1, 2, ., N}, soldiers occupy the N spots in that order. The PU spot is occupied before the Pli+1 spot. When a soldier occupies a spot, he is connected to his nearest soldier already placed to his left. If there is no soldier to his left, he is connected to the left tower. The same is the case with right side. A connection between two spots requires a wire of length equal to the distance between the two. The realm has already purchased a wire of M miles long from Nokia, possibly the wire will be cut into smaller length wires. As we can observe, the total length of the used wire depends on the permutation of the spots P. Help the realm in minimizing the length of the unused wire. If there is not enough wire, output -1. Example Input: 4 38 39 5 25 Output: -1 325

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%
Give the Code in Python and as short as possible and simple to understand
To protect people from evil, a long and tall wall was constructed a few years ago. But just a
wall is not safe, there should also be soldiers on it, always keeping vigil. The wall is very
long and connects the left and the right towers. There are exactly N spots (numbered 1 to
N) on the wall for soldiers. The Kh spot is K miles far from the left tower and (N+1-K) miles
from the right tower. Given a permutation of spots P of {1, 2, .., N}, soldiers occupy the N
spots in that order. The PIth spot is occupied before the Pli+1) spot. When a soldier
occupies a spot, he is connected to his nearest soldier already placed to his left. If there is
no soldier to his left, he is connected to the left tower. The same is the case with right side.
A connection between two spots requires a wire of length equal to the distance between the
two. The realm has already purchased a wire of M miles long from Nokia, possibly the wire
will be cut into smaller length wires. As we can observe, the total length of the used wire
depends on the permutation of the spots P. Help the realm in minimizing the length of the
unused wire. If there is not enough wire, output -1.
Example
Input:
4
24
5 25
Output:
-1
332N 5
Transcribed Image Text:Give the Code in Python and as short as possible and simple to understand To protect people from evil, a long and tall wall was constructed a few years ago. But just a wall is not safe, there should also be soldiers on it, always keeping vigil. The wall is very long and connects the left and the right towers. There are exactly N spots (numbered 1 to N) on the wall for soldiers. The Kh spot is K miles far from the left tower and (N+1-K) miles from the right tower. Given a permutation of spots P of {1, 2, .., N}, soldiers occupy the N spots in that order. The PIth spot is occupied before the Pli+1) spot. When a soldier occupies a spot, he is connected to his nearest soldier already placed to his left. If there is no soldier to his left, he is connected to the left tower. The same is the case with right side. A connection between two spots requires a wire of length equal to the distance between the two. The realm has already purchased a wire of M miles long from Nokia, possibly the wire will be cut into smaller length wires. As we can observe, the total length of the used wire depends on the permutation of the spots P. Help the realm in minimizing the length of the unused wire. If there is not enough wire, output -1. Example Input: 4 24 5 25 Output: -1 332N 5
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY