Summoners Incantation | Coding

π§ββοΈ Summoner’s Incantation β Write-Up | Cyber Apocalypse 2025
| Details | |
|---|---|
| URL | [Hack The Box :: Summoners Incantation |
| Difficulty | Easy |
| Team Rank | 541 / 8129 |
| CTF Date | 26 Mar, 13:00 |
π Challenge Description:
Deep within the ancient halls lies the secret of the Dragonβs Heartβa power that can only be unlocked by combining magical tokens in just the right way. The tokens are delicate: if two adjacent tokens are combined, their energy is lost.
Your quest is to determine the maximum amount of energy that can be harnessed by selecting tokens such that no two selected tokens are adjacent.
This challenge is essentially equivalent to computing the maximum sum of non-adjacent numbers in a list.
π§© Input Format:
- A single line representing a Python-style list of integers:
Example:
[3, 2, 5, 10, 7]
π― Output Format:
- A single integer β the maximum sum obtainable by selecting non-adjacent numbers from the list.
π§ Solution Approach:
This is a Dynamic Programming (DP) problem, commonly known as the House Robber Problem.
We maintain a DP array where:
dp[i]represents the maximum sum we can get up to indexi, without selecting adjacent elements.
The recurrence relation is:
dp[i] = max(dp[i-1], dp[i-2] + tokens[i])
dp[i-1]means we skip the current token.dp[i-2] + tokens[i]means we take the current token and add it to the best result two steps back (to avoid adjacency).
We build the solution from the beginning of the list and use the last value in the DP array as our answer.
π» Final Code:
input_text = input() # Example: "[3, 2, 5, 10, 7]"
tokens = eval(input_text)
if not tokens:
print(0)
elif len(tokens) == 1:
print(tokens[0])
else:
dp = [0] * len(tokens)
dp[0] = tokens[0]
dp[1] = max(tokens[0], tokens[1])
for i in range(2, len(tokens)):
dp[i] = max(dp[i - 1], dp[i - 2] + tokens[i])
print(dp[-1])
β Sample Input & Output:
Input:
[3, 2, 5, 10, 7]
DP State Progression:
dp[0] = 3
dp[1] = max(3, 2) = 3
dp[2] = max(3, 3+5) = 8
dp[3] = max(8, 3+10) = 13
dp[4] = max(13, 8+7) = 15
Output:
15
π Flag
‘‘HTB{SUMM0N3RS_INC4NT4T10N_R3S0LV3D_a59d50e9a6748f796931e35319bf7477}’’
