#!/usr/bin/perl -w

use v5.36;
use strict;
use experimental 'multidimensional';

my $tsc = 1;
my $x = 1;
my $prev_gen = 0;
my $sum = 0;
while (<>) {
	my ($op, $num) = /^(\w+)(?: (-?\d+))?/;
	# say "$tsc $x  $op $num";
	$tsc++;
	my $gen = int(($tsc+20)/40);
	if ($gen != $prev_gen) {
		$prev_gen = $gen;
		my $str = $x * ((40*$gen)-20);
		$sum += $str;
		# say "$tsc x $x = ", $str;
	}
	if ($op eq 'addx') {
		$x += $num;
		$tsc++;
	}
}

say $sum;

