Longitudinal distribution of initial energy density and directed flow of charged particles in relativistic heavy-ion collisions