#K67007. Maximum Power Consumption with Non-Adjacent Devices
Maximum Power Consumption with Non-Adjacent Devices
Maximum Power Consumption with Non-Adjacent Devices
You are given n devices, each consuming a certain amount of power. Your task is to determine the maximum total power consumption that can be achieved by selecting a subset of these devices. However, there is a constraint: you cannot pick two devices that are adjacent in the given list.
Formally, given an array powers of n integers where each element represents the power consumption of a device, choose a subset such that no two selected devices are consecutive. The objective is to maximize the sum of the selected devices' power consumption.
For example, if n = 4 and powers = [4, 1, 1, 9], the maximum power consumption is 13 (selecting the first and the last devices).
Solve this problem by reading from standard input and writing the result to standard output.
inputFormat
The input consists of two lines:
- The first line contains a single integer n, the number of devices.
- The second line contains n space-separated integers representing the power consumption of each device.
outputFormat
Output a single integer, which is the maximum power consumption achievable by selecting non-adjacent devices.
## sample4
4 1 1 9
13