Optimal Sizing and Safe Management of Lithium-Ion Batteries in High Voltage Power Systems