Per your logic, all countries in the world must dedicate absolutely all that can be spent on building up their militaries. Because having a strong military is the only possible way to not get invaded, bombed to pieces and/or occupied
This further requires all countries to take all possible action to develop nuclear weapons.
That is the only way they can be safe right?
If 193 countries in the world have nuclear weapons, then there would finally be peace. Well each country would have to continuously innovate and create more lethal forms, and new forms of delivery to stay safe and maintain peace.
No, they don't the to "dedicate absolutely all that can be spent", they just need to spend enough to deter potential enemies from attacking them. Or, have allies strong enough to offer that deterrence on their behalf.
But if you get conquered by a hostile country they'll be happy to strip you of your resources to fuel their war machine, and they won't really care about negative consequences to education and healthcare. Do you think Poland and Korea had much bargaining power to ask Germany and Japan to dedicate less resources towards war and more towards education and healthcare?
Again, you don't get to choose when war comes. If Ukrainians had opted to neglect their defense sector, where would they be now?